有谁知道如何在第一次异步通信的完成块中启动另一个新的异步方法的最佳实践?
我正在测试代码,以便在与Facebook的另一个异步通信的完成回调中异步调用NSFetchRequest(coz STACKMOB iOS SDK与服务器内部同步) . 代码的执行突然终止于NSFetchRequest行 . 我意识到它无法正常工作的原因之一 . 我想只要调用[managedObjectContext executeFetchRequest:fetchRequest error:&error],就会从内存中释放完成块 . 但我不知道解决它的更好的解决方案 . 谢谢你的帮助 .
SDK使用:
- (void)queueRequest:(NSURLRequest *)请求选项:(SMRequestOptions *)选项onSuccess:(SMFullResponseSuccessBlock)onSuccess onFailure:(SMFullResponseFailureBlock)onFailure
https://github.com/stackmob/stackmob-ios-sdk/blob/master/Classes/SMDataStore%2BProtected.m
我试过了:
-
How do I wait for an asynchronously dispatched block to finish?
-
NSURLConnection sendAsynchronousRequest:queue:completionHandler: making multiple requests in a row?
:
- (IBAction)checkFacebookInfo:(id)sender
{
//completion block of facebook info
void(^onCompleteBlock)(NSDictionary*) = [[^(NSDictionary* userInfo)
{
NSManagedObjectContext *managedObjectContext = nil;
managedObjectContext = [[SingletonCoreData sharedManager] managedObjectContext];
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] initWithEntityName:@"User"];
//for STACKMOB, customized NSFetchRequest internally sync to the server. It is Asynchronous method.
NSArray *results = [managedObjectContext executeFetchRequest:fetchRequest error:&error];// failed
//Not reached here
//set userInfo to results here
} copy] autorelease];
//invoke onCompleteBlock after executing asynchronously, client(SMClient object for STACKMOB)
[client getLoggedInUserFacebookInfoWithOnSuccess:onCompleteBlock onFailure:^(NSError *error)
{
NSLog(@"No user found");
}];
}
Edited: 我试过下面写的,然后它成功运作 . 但我觉得它很慢 . 我将部分代码放入'dispatch_async'块 . 我在等待任何其他更好的解决方案 .
- (IBAction)checkFacebookInfo:(id)sender
{
//completion block of facebook info
void(^onCompleteBlock)(NSDictionary*) = ^(NSDictionary* userInfo)
{
dispatch_queue_t gQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_async(gQueue, ^{
NSManagedObjectContext *managedObjectContext = nil;
managedObjectContext = [[SingletonCoreData sharedManager] managedObjectContext];
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] initWithEntityName:@"User"];
//for STACKMOB, customized NSFetchRequest internally sync to the server. It is Asynchronous method.
NSArray *results = [managedObjectContext executeFetchRequest:fetchRequest error:&error];// success
//set userInfo to results here
});
};
//invoke onCompleteBlock after executing asynchronously, client(SMClient object for STACKMOB)
[client getLoggedInUserFacebookInfoWithOnSuccess:onCompleteBlock onFailure:^(NSError *error)
{
NSLog(@"No user found");
}];
}
1 回答
如果您尝试同步多个异步操作(通过网络或其他方式),我不能建议使用promises . 对于承诺的一般性介绍,来自Los Techies的最新博客文章相当不错:
http://lostechies.com/derickbailey/2012/07/19/want-to-build-win8winjs-apps-you-need-to-understand-promises/
也就是说,有一个很好的库叫
KSPromise
,我在iOS上使用了很多:https://github.com/kseebaldt/deferred使用promises,您可以在一个或多个异步调用完成后进行获取请求 . 例如,只有在所有依赖的承诺都已解决后,才会解决加入承诺!我想你会发现使用promises会使你的代码变得更有条理,更容易阅读,特别是如果你正在做一些复杂的事情 .
也就是说,这是一个相当人为的例子,展示了使用promises编写代码的可能方式(与Facebook iOS 3.1 SDK编写):
最后一件事:在使用带有线程的核心数据和
dispatch_async
时要非常非常小心 . 您可能会遇到各种难以调试的并发问题 .