Salah Salah - 1 year ago 91
iOS Question

Swift Parse how to make app works offline?

I'm working on local parse with swift 3.0

I'm doing querys to get results from server. but if there's no connection it wont show last results we got because losing connection.

so what i want to do is to save query results to view it if there is no connection

this is the query:

var query = PFUser.query()

query = PFQuery(className: "_User")

// query?.fromLocalDatastore()

query!.whereKey("objectId", equalTo: PFUser.current()!.objectId!)
query!.findObjectsInBackground {
(objects , error) -> Void in

if error == nil {

for object in objects! {

self.usernamelbl.text = object["username"] as! String

if let userp = PFUser.current()?["photo"] as? PFFile {

userp.getDataInBackground {
(imageData, error) -> Void in

if error == nil {

self.profilepic.image = UIImage(data: imageData!)!

Now how can i save the results and view them offline also if app closed?
Any help will be appreciated

Answer Source

It's possible to set the caching policy of specific PFQuery calls. To save a copy to disk, and rely on that before making another network hit, you set the kPFCachePolicyCacheElseNetwork policy.

However according to this Parse question, there is apparently a pretty strict limit on the size these caches are allowed to be. I'm not sure if those still apply in the open source version of Parse, but if you want to save more information to disk, it might be appropriate to use a more dedicated data persistence framework, like Core Data, SQLite, or Realm (Full disclosure: I work for Realm. :) )

For the purposes of image files, I'd recommend you manually manage the caching of that on disk, instead of storing it in Parse's cache (Due to the size constraints). There are some great image caching libraries out there (Like PINCache) that make it very easy.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download