3

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!

2
  • findObjectsInBackgroundWithBlock is an synchronous method so you must as connection to query data. what are you trying to do Commented Sep 2, 2015 at 22:52
  • I hope you meant asynchronous @Lamar Commented Sep 3, 2015 at 1:20

1 Answer 1

2

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

Sign up to request clarification or add additional context in comments.

3 Comments

Perfect thank you. You're right I got fixated on localdatastore which, when looking at the documentation, didn't seem right for the purpose. Thanks for the tips... I was taught that about 10 years ago and have since forgotten the conventions to follow! It does make it prettier
Great @Tom it's Stack Overflow etiquette to accept answers that resolve any outstanding issues within your question. If this does help, please accept the answer by selecting the check mark next to it. meta.stackexchange.com/a/5235 I'm not really concerned with it, but other users who are working hard to earn rep would like to be praised. Additionally, it lets other users who have the same problem know that the answer is the right one that they should implement in their own project as well as reducing duplicate questions with same topics.
Done. Thanks for explaining - appreciate your help and patience!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.