Image within PFFile will be uploaded to parse instead of being saved into the local datastorage
Asked Answered
E

1

2

I am using swift with the Parse Framework (1.6.2). I am storing data into the local datastorage. The data consists of a few strings and an image. This image will be stored within a PfFile and then will be added to the PfObject.

Now I wondered why the image doesn't get saved until I noticed I needed to call the save() method of the PFFile. Problem is that the Image then seems to be uploaded to Parse which I don't want. I want it to be stored on the local device..

Is there a solution for this?

Thanks

Edit:

The code works like this:

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()
Euh answered 25/1, 2015 at 12:6 Comment(5)
when you save the image in PFFile, and you attach it to PFObject it will automatically get upload to Parse. What you can do is, store the image locally get the path to the image. Attach path to the PFObject as string. Save the PFObject. Done !!!! you will have the path from PFObject when you want to pull it back. You can also save it core data. Local storage depends on your needs. :)Protostele
I don't really understand. The (new) local data storage is actually made for storing data offline. Everything I store into the local data storage won't be uploaded until I tell parse to do so (with saveEventually) So I don't understand why adding an image is something different?Euh
did you call save on PFObject ?Protostele
see the example code I've addedEuh
@Euh Absolutely agree this is ridiculous. If I can't store data in a local datastore what bloodyyyyyyyyyyy use is it?Quadrillion
P
1

Okay, take a look here Save eventually on PFObject with PFFile (Parse Local Datastore)?. One this is for sure, if you call save on PFFile it will get save to online datastore. So you should use PFFile.save(). I think best option for you is to save the file in some folder. locally and save that path in PFObject. Parse documentation just say this

"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."

It recursively calls .pin() on the other objects in your main PFObject. But if you take a look at PFFile API doc it doest have a .pin() which means it doesn't support saving PFFile to local datastore. So I would say you should save them in some directory and save path in your PFObject.

Update

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.
Protostele answered 25/1, 2015 at 12:44 Comment(8)
This is such a crap. I can't understand why they've not just added this feature. I mean it's nowhere in the docu that images will be 'silently' saved to the server...Euh
parse local datastore is really new. Actually you are not able to call PFObject saveEventually() on a PFObject with a PFFile. And keep in mind that save will always trys to save in online data store. That is the reason why they came up with a new term "pin" to save it locally.Protostele
I would expect to at least have a pin method within each mainly used Object like PfFile and PFObject. I mean does it make sense to say: Yes you can store data locally as long as they are not binary?Euh
@Euh I answered the other question in reference to this, I was directed here by my questioner, I feel like the reason is because PFFiles are cached see here for numerous reasons so to pin to local disk of an already cached file in the local disk would be redundant good read my suggestion would be to utilize their cache policy kPFCachePolicyCacheThenNetwork where needed another good readCurve
I've changed the applications behavior to now store files locally and just save the file path within the PFObject. That works pretty smooth, much faster than doing this with ParseEuh
That's what I recommend in my answer @Euh a lot more control over the process f you do itCurve
Here's a little extension I made for saving PFFIle's in conjunction with Local Datastore: github.com/AlexEKoren/AKLocalDatastoreGorrian
@ AlexKoren My scenerio might be a little different, but I'm trying to save the data and files locally fist, then at a later time, when I have an internet connection, sync with Parse, will this method still work with the solution you are discussingRael

© 2022 - 2024 — McMap. All rights reserved.