我在解决后台队列中的modalviewcontroller时遇到了问题。这是代码
- (void)cleanUpPictures
{
dispatch_queue_t queue = dispatch_queue_create("ch.thurboapp.cleanup", NULL);
dispatch_async(queue, ^{
NSManagedObjectContext *context = [[NSManagedObjectContext alloc] init];
[context setPersistentStoreCoordinator:[self.managedObjectContext persistentStoreCoordinator]];
//register for core data save notification
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(mergeChanges:)
name:NSManagedObjectContextDidSaveNotification
object:context];
[self deleteAllObjects:@"Thumbnail" inContext:context];
[self deleteAllObjects:@"MenuThumbnail" inContext:context];
[self deleteAllObjects:@"PictureData" inContext:context];
[context release];
dispatch_async(dispatch_get_main_queue(), ^{
[[NSUserDefaults standardUserDefaults] setObject:[NSDate date] forKey:LastCleaned];
[[NSUserDefaults standardUserDefaults] synchronize];
[self.parentViewController dismissModalViewControllerAnimated:YES];
});
});
dispatch_release(queue);
}
[self.parentViewController dismissModalViewControllerAnimated:YES];
在主线程内调用,但没有效果。有人知道这个问题的解决方案吗?
非常感谢帮助。
答案 0 :(得分:0)
我不太熟悉UI代码在块中的交互方式,但您是否尝试过更简单的performSelectorOnMainThread?
答案 1 :(得分:0)
尝试使用调度组在第一个工作块完成时通知您:
- (void)cleanUpPictures
{
dispatch_queue_t queue = dispatch_queue_create("ch.thurboapp.cleanup", NULL);
// To know when we're done, we need to associate work in this queue
// with a group
dispatch_group_t group = dispatch_group_create();
dispatch_group_async(group, queue, ^{
NSManagedObjectContext *context = [[NSManagedObjectContext alloc] init];
[context setPersistentStoreCoordinator:[self.managedObjectContext persistentStoreCoordinator]];
//register for core data save notification
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(mergeChanges:)
name:NSManagedObjectContextDidSaveNotification
object:context];
[self deleteAllObjects:@"Thumbnail" inContext:context];
[self deleteAllObjects:@"MenuThumbnail" inContext:context];
[self deleteAllObjects:@"PictureData" inContext:context];
[context release];
});
// When the last block of work in this group finishes
// dispatch_group_notify fires
dispatch_group_notify(group, dispatch_get_main_queue(), ^{
[[NSUserDefaults standardUserDefaults] setObject:[NSDate date] forKey:LastCleaned];
[[NSUserDefaults standardUserDefaults] synchronize];
[self.parentViewController dismissModalViewControllerAnimated:YES];
});
dispatch_release(queue);
dispatch_release(group);
}