-->

how to use local datastore to save and query data

2019-08-31 05:53发布

问题:

In my app. There will be user list and message list as my code below
message list code (load the list from parse)

    @IBOutlet var messageTableView: UITableView!
var messageArray:[String] = ["Lope"]
override func viewDidLoad() {
    super.viewDidLoad()
    retrieveMessages()
    }
func retrieveMessages() {
    var query = PFQuery(className:"Messages")
    var user = PFUser.currentUser()
    query.whereKey("user", equalTo:user.objectId)
    query.findObjectsInBackgroundWithBlock { [weak self]
        (objects:[AnyObject]?, error:NSError?) -> Void in
        println(objects)
        println("succeed")
        let messages = objects
        for object in objects!{
            if let message = object["messageTextColumn"] as? String {
                println(object)
                self?.messageArray.append(message)

            }

        }

         self?.tableView.reloadData()
    }

}






override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return messageArray.count
}


override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("messageCell", forIndexPath: indexPath) as UITableViewCell
        cell.textLabel?.text = messageArray[indexPath.row]
        return cell
} 

add message code (add new message to parse)

class addMessageViewController: UIViewController {

@IBOutlet weak var addMessageText: UITextField!
@IBAction func addMessage(sender: AnyObject) {

    var newMessage = addMessageText.text

    let message = PFObject(className: "Messages")
    var user = PFUser.currentUser()
    message["messageTextColumn"] = newMessage
    message["user"] = user.objectId

    message.saveInBackgroundWithBlock {(success: Bool, error: NSError?) -> Void in
        if (success) {
            println("added to Message Class")
           println(user)
            message.saveInBackground()
        } else {
            // Error saving message
        }
    }
}

I want to use parse local datastore to store these data in my app locally so that my app won't have to use the internet connect all the time and when the user is not connect to the internet the user list and message list will still appear.

The problem is I don't know what method in local datastore should I use where should I put the local datastore code in "add message code" to save the new message and in "message list code" to query it to my app locally and if there's any update, It will do later after our local "message list" has been loaded. Any help is appreciated. Thanks!

回答1:

To begin with Parse data store, you need to opt in from your app delegate:

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        // Override point for customization after application launch.


        // Opt in for Parse Local data store *Before Parse.setApplicationId*
        Parse.enableLocalDatastore()

        Parse.setApplicationId("YOUR PARSE APP ID",
            clientKey: "YOUR PARSE CLIENT ID")

       //... other code that you might need when app did finish launching

      return true
 }

Later when you save a new message you will use:

message.saveEventually()

This will save in the local data store, and eventually (when internet will be available) in the remote data store.

From here you might also be interested in the use of Parse data pinning. See Parse doc for more.

Hope this helps