考慮下面的代碼:返回完成一個塊?
+(Tag *) addNewTagwithName:(NSString *)tagName Error:(NSError **)addError {
AppDelegate *delegate = (AppDelegate *) [[UIApplication sharedApplication]delegate];
Tag *newTag = [NSEntityDescription insertNewObjectForEntityForName:@"Tag" inManagedObjectContext:delegate.managedObjectContext];
newTag.tagName = tagName;
//no error save to parse
PFObject *tag = [PFObject objectWithClassName:@"Tag"];
tag[@"tagName"] = newTag.tagName;
[tag saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
if(error) {
*addError = error;
} else {
dispatch_async(dispatch_get_main_queue(), ^{
NSError *error = nil;
newTag.tagId = tag.objectId;
if(![delegate.managedObjectContext save:&error]) {
*addError = error;
}
});
}
}];
return newTag;
}
都有些什麼方法來確保newTag僅之後執行newTag.tagId = tag.objectId回來了?
您能否說一下'saveInBackgroundWithBlock?'核心數據中的線程安全性應該是一個問題。 –
是否有任何理由以異步方式和背景保存? –
密切相關:[塊內函數的返回值](http://stackoverflow.com/q/17642535) –