我正在使用Swift与Parse Framework(1.6.2)。我将数据存储到本地数据存储中。数据由几个字符串和一个图像组成。该图像将存储在PfFile中,然后将添加到PfObject中。
现在我想知道为什么图像没有被保存,直到我注意到我需要调用PFFile的save()方法。问题是图像似乎上传到Parse,我不想要。我希望它存储在本地设备上..
有解决方法吗?
由于
编辑:
代码的工作原理如下:
var spot = PFObject(className: "Spot")
let imageData = UIImageJPEGRepresentation(spotModel.getPhoto(), 0.05)
let imageFile = PFFile(name:"image.jpg", data:imageData)
spotObj.setObject(spotModel.getCategory(), forKey: "category")
spotObj.setObject(imageFile, forKey: "photo")
//if I simply would do this, the PFObject will be saved into the local datastorage, but the image won't be saved
spotObj.pin()
//if I do this, the image will be saved also BUT it won't be saved into the local data storage but will be uploaded to parse
imageFile.save()
spotObj.pin()
答案 0 :(得分:1)
好的,看看Save eventually on PFObject with PFFile (Parse Local Datastore)?。 这是肯定的,如果你在PFFile上调用save,它将保存到在线数据存储区。所以你应该使用PFFile.save()。我认为最好的选择是将文件保存在某个文件夹中。本地并在PFObject中保存该路径。解析文档只是说这个
"Pinning a PFObject is recursive, just like saving, so any objects that are pointed to by the one you are pinning will also be pinned. When an object is pinned, every time you update it by fetching or saving new data, the copy in the local datastore will be updated automatically. You don't need to worry about it at all."
它递归调用主PFObject中其他对象的.pin()。但是如果你看一下PFFile API doc,它就会有一个.pin(),这意味着它不支持将PFFile保存到本地数据存储区。所以我会说你应该将它们保存在某个目录中并保存PFObject中的路径。
更新
save:
Saves the file synchronously and sets an error if it occurs.
- (BOOL)save:(NSError **)error
Parameters
error
Pointer to an NSError that will be set if necessary.
Return Value
Returns whether the save succeeded.