简体   繁体   中英

Parse Local DataStore not showing pinned data

I have tried to make sure that all the recommended steps are followed in implementing Parse local data store but either pinning seems to be not working or querying the pinned objects is not working. I have tried multiple options. Below is my code for the view controller and also I have enabled the datastore etc in the app delegate file as well (using the base parse starter project). Please advise me where is the problem.

Output in my console - i am able to fetch data from the parse server but am either not able to pin it properly or retrieve it properly or something else..

 Success 8888 
Optional([])
Push notifications are not supported in the iOS Simulator.
success 7777
Optional([<Restaurant: 0x7f98ca521f60, objectId: 0rRZNCndje, localId: (null)> {
    Name = time;
}])

Thanks for the help!

import Foundation
import Parse
import ParseUI
import UIKit
import Foundation

class RestaurantAdmin: ViewController {
    func getDataFromLocalDataStore() {
        var userName = PFUser.currentUser()?.username
        var messages2: [AnyObject]!
        var query2: PFQuery = PFQuery(className: "Restaurant")
        query2.fromLocalDatastore()
        query2.whereKey("Owner", equalTo: userName!)
        query2.findObjectsInBackgroundWithBlock { (objects, error) -> Void in
            if (error == nil) {
                messages2 = objects
                println(" Success 8888 ")
                println(objects)
            }
            else {
                println("Fail 8888")
            }
        }
    }

    func refreshRestaurantDataFromServer() {
        var userName = PFUser.currentUser()?.username
        var query = PFQuery(className: "Restaurant")
        query.whereKey("Owner", equalTo: userName!)
        query.selectKeys(["Name"])
        var messages: [AnyObject]!
        query.findObjectsInBackgroundWithBlock { (objects, error) -> Void in
            if (error == nil) {
                PFObject.pinAllInBackground(objects, block: nil)
                println("success 7777")
                println(objects)
            }
            else {
                println("error 7777")
            }
        }
    }

    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        if (PFUser.currentUser()?.username != nil) {
            refreshRestaurantDataFromServer()
        }
        getDataFromLocalDataStore()
    }
}

Since you are using query.selectKeys(["Name"]) the whole object is not downloaded. Now you are saving this partial object and then querying using query2.whereKey("Owner", equalTo: userName!) . The saved object does not contain the "Owner" key.

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