I've enabled local datastore in app delegate
, I've did a query but how can i save that objects to view them if there's no internet
?
Here is my query :
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) }
}
}
Now how can i do a local query
to view this objects
?
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.