[英]Parse nested saveInBackgroundWithBlock in iOS
我正在使用將Parse用於服務器后端的iOS項目。 作為我的代碼的一部分,我具有以下saveInBackgroundWithBlock嵌套塊。
// Save PFFile
[imageFile saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
...
[userPhoto saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
if (!error) {
self.profileDictionary[@"picture"] = userPhoto;
NSLog(@"%@", self.profileDictionary);
NSMutableDictionary *userProfile = self.profileDictionary;
[[PFUser currentUser] setObject:userProfile forKey:@"profile"];
[[PFUser currentUser] saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
if (!error) {
NSLog(@"Saving User Profile Succeded\n\n\n\n");
// If user's info is saved, then let's just segue to the actual app
[self performSegueWithIdentifier:@"profileToMain" sender:self];
} else {
// Log details of the failure
NSLog(@"Error while saving profile: %@ %@", error, [error userInfo]);
}
}];
} else {
// Log details of the failure
NSLog(@"Error: %@ %@", error, [error userInfo]);
}
}];
}];
如您所見,代碼主要是3個saveInBackgroundWithBlock:
塊嵌套在一起。
一切工作到第三個街區為止。 我知道這是因為segue沒有執行。
該代碼的主要思想是一個接一個地保存東西。 換句話說,imageFile是一個PFFile
保存的PFFile,然后再保存一個userPhoto PFObject
,最后是已預先填充的userProfile與新的PFObject
一起保存。 此個人資料。
關於此掛起的任何評論? 如何在不導致掛起的情況下實現將對象保存到Parse的連續過程?
非常感謝您的幫助和時間。
PFObject SaveAll功能不是嵌套有時對我有用的功能,您可以在其中傳遞對象數組,並按照它們在數組中出現的順序進行保存,然后將它們全部保存,並且一旦它們嵌套,您仍然可以執行嵌套的代碼塊一切成功。
[PFObject saveAllInBackground:@[Object1, Object2] block:^(BOOL succeeded, NSError *error) {
if(succeeded){
//Your code to execute once finished
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.