简体   繁体   中英

Saving and loading UITableView array - iOS Swift Parse

I have the following; which fetches the data from the Parse backend and loads the class instances into a NSArray to plug into a UITableView .

var ObjectIDClass = PFQuery(className: "TestObject")

ObjectIDClass.findObjectsInBackgroundWithBlock({
  (ObjectsArray : [AnyObject]?, error: NSError?) -> Void in

  var ObjectIDs = ObjectsArray as! [PFObject]
  for i in 0...ObjectIDs.count-1 {
    self.iDArray.append(ObjectIDs[i].valueForKey("objectId") as! String)
    self.NameArray.append(ObjectIDs[i].valueForKey("PDFName") as! String)
    self.tableView.reloadData()
  }
})

This only works with network connection and as soon as the application is closed and then reopened without network, the table is blank. What I am aiming for is to store the data in the Local Datastore and if there is no connectivity, load from the Local Datastore . I can't figure it out using pin , but sure that is how it is done. Any help would be hugely appreciated!

Instead of focusing strictly on Local Datastore , which isn't a bad thing. I just feel you are using that solely because you want to have access to that data, say when the user is in airplane mode, or when there are just low network conditions. If that is the case, then you can take advantage of Parse's built in cache system. The PFQuery cache by default is set to ignore cache. This means, none of the information incoming from the server is stored in a temp folder on the users hard disk (exactly what your looking for). So you have to explicitly tell the objects to be cached on device using one of their many caching policies

For you, this will be good:

query.cachePolicy = .CacheElseNetwork
// Results were successfully found, looking first on the
// disk and then on network.
query.findObjectsInBackgroundWithBlock {

You can review other ways to cache with these options : https://www.parse.com/docs/ios/guide#queries-caching-queries

Small side note, please don't uppercase your variables. It doesn't follow proper naming conventions. ObjectIdDClass should resemble, somewhat, what your going to do with it's class, so it should be called query or something. And ObjectsArray should be objects because you already know it's an array and you don't capitalize anything that's not a class, usually. It makes for easier reading

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM