2013-02-17 23 views
5

我的應用程序爲每個用戶創建一個對象(PFUSER),併爲他們參與的每個事件創建一個(PF)對象。那麼我有兩個與該事件相關的文件。我將第一個文件保存到PFFile,然後將它關聯到事件pfobject。當我使用塊並在後臺執行此操作時,如何確保控件繼續爲第二個文件執行相同操作?在iOS上使用分析,如何將兩個PFFiles保存到背景中的PFObject

我是新來塊,所以也許它會更清楚爲什麼它不使用回調,但它似乎塊運行保存在另一個線程和當前一個被放棄之前採取下一步。

當然,我希望將這兩個作爲「最終保存」以允許脫機使用。

任何指導/例子,你可以指向我非常感謝。

謝謝!

回答

11

saveEventually不支持PFFiles尚未;它需要更多的智慧來處理重新啓動之間的恢復上傳。然而,一個已經可用的技巧是,PFObject知道如何保存子項,包括PFFiles。你可以說:

PFUser *user = PFUser.currentUser; 
user[@"icon"] = [PFFile fileWithData:iconData]; 
user[@"iconThumb"] = [PFFile fileWithData:iconThumbData]; 
[user saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) { 
    // user will automatically save its files & only call this once the 
    // entire operation succeeds. 
}]; 
+0

感謝這個想法。保存最終是好的,因爲實際上,即使沒有連接,我的應用程序似乎也可以調用它。目前我只是爲兩個文件中的每一個做一個「saveInBackground」。這看起來更好,在每個文件保存後,我目前正在做一個PFQuery來獲得我想將文件關聯並更新它的對象 - 這樣做一次看起來好多了(並且意味着我可以停止擔心將文件附加到正確的對象!)。謝謝。 – hangzhouharry 2013-02-20 06:24:21

1

我不是100%,你的意思,因爲你沒有發佈任何代碼,但如果你想多PFFile關聯到PFObject這是所有你必須做我想像:

PFObject *object = [PFQuery getObjectOfClass:@"MyFile" objectId:id]; 
[object addObject:profilePicture forKey:@"Photo"]; 
[object addObject:coverPicture forKey:@"PhotoCover"]; 
[object saveEventually]; 

Parse's documentation好像saveEventually你想要做什麼:

Saves this object to the server at some unspecified time in the future, even if Parse is currently inaccessible. Use this when you may not have a solid network connection, and don’t need to know when the save completes. If there is some problem with the object such that it can’t be saved, it will be silently discarded. If the save completes successfully while the object is still in memory, then callback will be called.

+0

這是不行的,你會得到這個錯誤:無法saveEventually一個PFObject一個關係到一個新的,未保存的PFFile – 2015-01-17 12:38:19

1

由於目前沒有saveEvetually也不保存到本地數據存儲的支持,下面是PFObject的一類我使用至少離線救什麼可以保存或返回錯誤:

- (void) dr_saveWithCompletionHandler: (void(^)(NSError* error)) completionBlock { 

__block BOOL canSaveEventually = YES; 

[[self allKeys] enumerateObjectsUsingBlock:^(NSString* key, NSUInteger idx, BOOL *stop) { 
    id object = self[key]; 

    if ([object isKindOfClass:[PFFile class]]) { 
     PFFile* file = (PFFile*) object; 

     if (!file.url || file.isDirty) { 
      canSaveEventually = NO; 
     } 
    } 
}]; 

void (^localCompletionHandler) (BOOL, NSError*) = ^(BOOL succeeded, NSError *error) { 

    if (succeeded) { 
     if (completionBlock) completionBlock(nil); 

    } else { 
     if (completionBlock) completionBlock(error); 
    } 
}; 

if (canSaveEventually) { 
    [self saveEventually:localCompletionHandler]; 
} else { 
    [self saveInBackgroundWithBlock:localCompletionHandler]; 
} 

}

相關問題