有没有人知道如何在第一次异步通信的完成块中启动另一个新的异步方法的最佳实践?
我正在测试代码,以便在与Facebook的另一个异步通信的完成回调中异步调用NSFetchRequest(coz STACKMOB iOS SDK与服务器内部同步)。代码的执行突然终止于NSFetchRequest行。我意识到它无法正常工作的原因之一。 我想只要调用[managedObjectContext executeFetchRequest:fetchRequest error:& error],就会从内存中释放完成块。 但我不知道更好的解决方案来修复它。谢谢你的帮助。
SDK使用:
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");
}];
}
编辑: 我试过下面写的,然后它成功运作。但我感觉很慢。我将部分代码放入'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");
}];
}
答案 0 :(得分:0)
如果您尝试同步多个异步操作(通过网络或其他方式),我建议不要使用promises。对于承诺的一般性介绍,最近来自Los Techies的博客文章相当不错:
那就是说,有一个很好的库KSPromise
,我在iOS上使用了很多:https://github.com/kseebaldt/deferred
使用promises,您可以在一个或多个异步调用完成后进行获取请求。例如,只有在所有依赖的承诺都已解决后,才会解决加入承诺!我想你会发现使用promises会使你的代码变得更有条理,更容易阅读,特别是如果你正在做一些复杂的事情。
那就是说,这是一个相当人为的例子,展示了使用promises编写代码的可能方式(与Facebook iOS 3.1 SDK编写):
- (IBAction)checkFacebookInfo:(FBSession *)session {
KSDeferred *dfd = [KSDeferred defer];
FBRequest *meRequest = [[FBRequest alloc] initWithSession:session graphPath:@"/me"];
[meRequest startWithCompletionHandler:
^(FBRequestConnection *connection, NSDictionary<FBGraphUser> *user, NSError *error) {
if (error) {
[dfd.promise rejectWithError:error];
} else {
[dfd.promise resolveWithValue:user];
}
}];
[dfd.promise whenResolved:^(KSPromise *p) {
NSDictionary *userInfo = p.value;
NSManagedObjectContext *managedObjectContext = [[SingletonCoreData sharedManager] managedObjectContext];
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] initWithEntityName:@"User"];
NSError *error;
NSArray *results = [managedObjectContext executeFetchRequest:fetchRequest error:&error];
// Do whatever with results and userInfo here
}];
[dfd.promise whenRejected:^(KSPromise *p) {
NSError *e = (NSError *)p.value;
NSLog(@"Error: %@", [e localizedDescription]);
}];
}
最后一件事:在使用带有线程的核心数据和dispatch_async
时要非常非常小心。您可能会遇到各种难以调试的并发问题。