最终保存在PFObject上使用PFFile(解析本地数据存储)



>目标

我正在尝试保存具有PFFile作为属性的PFObject。我正在使用适用于iOS的新本地数据存储,因此我想使用saveEventually()方法保存此PFObject

问题所在

我遇到的问题是saveEventually()方法似乎不喜欢保存PFFiles。我试图在没有任何PFFile的情况下saveEventually()我的对象,效果很好。重新附加我的PFFile后,Xcode 抛出了几个断点通知(错误?),但没有终止应用程序,看起来一切顺利 - 但是对解析数据浏览器的检查确认保存没有通过。

在本地数据存储功能之前,我不相信这种保存是可能的 - 它会抛出"Unable to saveEventually a PFObject with a relation to a new, unsaved PFFile."错误。本地数据存储功能似乎已解决此问题,如iOS本地数据存储文档中所述:

"固定 PFObject 是递归的,就像保存一样,所以任何对象 由您要固定的那个指向的也将固定。 固定对象时,每次通过获取或更新对象时 保存新数据时,将更新本地数据存储中的副本 自然而然。你根本不需要担心。

我已将 SDK 更新到最新版本 (v1.6.2)。有什么想法吗?

PFFiles 仍然不支持saveEventually在这里看到

该页面最后更新 : 2015-01-23

您可以pinInBackgroundWithBlock,如果成功,则将 PFFile 保存到应用程序捆绑包中的临时文件夹,并在必要时将其删除或取消固定

我刚刚发布了一个允许保存最终 PFFile 的类。

你可以在这里找到它:

/*
     This example uses an UIImage, but this works with any file writable as NSData
     We begin by writing this image in our tmp directory with an uuid as name.
 */
UIImage *nyancat = [UIImage imageNamed:@"nyancat.jpg"];
NSData *imageData = UIImageJPEGRepresentation(nyancat, 0.5);
NSString *filename = [[NSUUID UUID] UUIDString];
NSURL *fileUrl = [PFFileEventuallySaver fileURLInTmpWithName:filename];
[imageData writeToURL:fileUrl atomically:YES];
 /*
     We create a PFObject (you can pass an array to below function if you need your file to be saved on several objects). If upload works on first time, do what you want with your file, like linking it on your PFobject.
     If saving fails, it'll be retried as soon as network is available, on this session or nexts launches of app.
     In that case, the pointer at key kPFFILE_MANAGER_OBJECT_FILE_KEY of your PFFObject will be set with the PFFile, then saved eventually within PFFileEventuallySaver
 */
PFObject *object = [PFObject objectWithClassName:kPFFILE_CONTAINER_OBJECT_CLASSNAME];
[[PFFileEventuallySaver getInstance] trySaveobjectAtURL:fileUrl associatedObjects:@[object] withBlock:^(PFFile *file, NSError *error) {
if(!error)
{
    NSLog(@"[First try, network is fine] File saved, saving PFObject");
    object[kPFFILE_MANAGER_OBJECT_FILE_KEY] = file;
    [object saveEventually];
    NSLog(@"Try again disabling your network connection");
}
else
{
    NSLog(@"No network, connect back your wifi, or relaunch app. Your file will be sent");
}
} progressBlock:^(int percentDone) {
    NSLog(@"[First try, network is fine] Sending file %d/100%%", percentDone);
}];

最新更新