Save Eventually on Pfobject with Pffile (Parse Local Datastore)

Save eventually on PFObject with PFFile (Parse Local Datastore)?

PFFiles still don't support saveEventually see here

That page was last updated : 2015-01-23

You could pinInBackgroundWithBlock and if successful save the PFFile to a temporary folder in you app bundle and delete it when necessary or unpinned

How do I store ParseObject in both Local Datastore and Parse Cloud on Objective-C iOs 8?

First of all you need to enable the use of local datastore:

[Parse enableLocalDatastore];

Next, I always save a new PFObject to the local datastore using

PFObject *userStat;
[userStat saveEventually];

This will both pin your object to the local datastore and save it to cloud (eventually). If you want to reset all of your locally stored data with what you have in the cloud, you can; first unpin all your local objects and then fetch all the remote objects and pin them locally:

[PFObject unpinAllObjects];
PFQuery *query = [PFQuery queryWithClassName:@"UserStats"];
[query whereKey:@"parent" matchesQuery:query];

return [[query findObjectsInBackground] continueWithBlock:^id(BFTask *task) {
if (task.error){
return nil;
}
return [[PFObject pinAllInBackground:task.result] continueWithBlock:^id(BFTask *task) {
return task;
}];
}];

Local queries can be done using:

 [query fromLocalDatastore];

Parse has good documentation of these methods here.

Parse iOS - How to save an PFObject locally and manually upload to server later?

If you have enabled the local datastore, calling pinObjectInBackground() on an object should make the object persist between sessions. Every time you make changes to the object that you want to save, call pinObjectInBackground() again. The object will not be saved to the server until you call saveEventually() or some other save function. Any query using fromLocalDatastore() will be able to fetch your locally stored object as long as it is pinned.

Update pinned PFObject in Parse Local Storage from Parse Cloud

I found out that both findObjectsInBackground and fetchAllInBackground will update any pinned object matching the objectId.

That is to say, when you create an object initially, it will not have an objectId, but you can still pin this object without successfully saving it. However, you can't find or fetch them until you successfully save it to the cloud. So in the code, you have to cherrypick these particular PFObjects out and update them some other ways or don't update them at all.

Swift Parse - local datastore and displaying objects in a tableview

It's always a good idea to avoid pointer lol ... so why not saving the userid or username with the specific object..
so change this line:

 parseLighthouse.setObject(PFUser.currentUser()!, forKey: "User")

TO

 parseLighthouse["username"] = PFUser.currentUser().username

Answer

NOW let's create a struct that contains the objectID and the Name outside of your Controller Class.

struct Data
{
var Name:String!
var id:String!
}

then inside of the Controller class, declare the following line of code globally

 var ArrayToPopulateCells = [Data]()

Then your query function will look like :

 func performQuery() {
let query = PFQuery(className: "ParseLighthouse")

query.fromLocalDatastore()
query.whereKey("User", equalTo: PFUser.currentUser()!)
query.findObjectsInBackgroundWithBlock { (objects, error) -> Void in
if error == nil {
// The find succeeded.
print("Successfully retrieved \(objects!.count) lighthouses.")
// Do something with the found objects
if let light = objects as? [PFObject] {
for object in light {
print(object.objectId)
print(object.objectForKey("Name"))
var singleData = Data()
singleData.id = object.objectId
singleData.Name = object["Name"] as! String

self.ArrayToPopulateCells.append(singleData)

}
}
} else {
// Log details of the failure
print("Error: \(error!) \(error!.userInfo)")
}
}

In the tableView numberOfRowinSection()

return ArrayToPopulateCells.count

In the cellForRowAtIndexPath()

       var data = ArrayToPopulateCells[indexPath.row]
cell.textlabel.text = data.objectID
cell.detailLabel.text = data.Name

VOila that should be it

swift parse how to save objects in local datastore and show them?

Before calling findObjectsInBackgroundWithBlock,

query.fromLocalDatastore()

This will force your query to pull from localDataStore. Your code should look like below:

query.fromLocalDatastore()
query.findObjectsInBackgroundWithBlock {
(objects, error) -> Void in
if error == nil && objects != nil
{
self.messages = objects!
for object in objects! {
inboxphotos.append(object.objectForKey("photo") as! PFFile)
inboxusers.append(object.objectForKey("username") as! String) }
}
}

In order to save objects locally, you must first enable the localDataStore:

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
Parse.enableLocalDatastore()
Parse.setApplicationId("parseAppId", clientKey: "parseClientKey")
}
}

The recommended way for saving objects with localDataStore is to use saveEventually:

myObject.saveEventually()

You may also use .pinInBackground() to keep objects in the localDataStore "permanently". Pinning objects makes sure that local objects are automatically updated when you fetch them again from the server. It is an ideal way to implement offline caching of server data.



Related Topics



Leave a reply



Submit