如何使用本地数据存储区以解析和快速方式保存和查询数据 [英] how to use local datastore to save and query data with parse and swift

查看:168
本文介绍了如何使用本地数据存储区以解析和快速方式保存和查询数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用中.下面将有用户列表和消息列表作为我的代码
消息列表代码(从解析中加载列表)

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
} 

添加消息代码(添加新消息以进行解析)

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!

推荐答案

要开始解析数据存储,您需要从应用程序委托中选择加入:

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
 }

稍后,当您保存新的message时,将使用:

Later when you save a new message you will use:

message.saveEventually()

这将保存在本地数据存储中,并最终(当有Internet可用时)保存在远程数据存储中.

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

从这里开始,您可能还对使用Parse数据固定感兴趣. 有关更多信息,请参见解析文档.

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

希望这会有所帮助

这篇关于如何使用本地数据存储区以解析和快速方式保存和查询数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆