简体   繁体   中英

Parse: Does not query saved objects in local datastore

I am currently developing a inventory app. My goal is to retrieve objects from Parse and then saving onto the local datastore. Querying objects from Parse and saving them works (because of the console message) but querying later on from the local datastore, does not retrieve anything! Here's my code:

let query = PFQuery(className: "Publication")
    query.limit = 150
    query.selectKeys(["publication_id","publication_Type","publication_Name"])
    dispatch_async(dispatch_get_main_queue()) { () -> Void in
        query.findObjectsInBackgroundWithBlock({ (pubObject, error) -> Void in
            if error == nil {
                print("Succesfully retrieved \(pubObject!.count)")

                PFObject.saveAllInBackground(pubObject, block: { (success, error) -> Void in
                    print("Saved \(pubObject!.count) in local DataStore")
                })

            }
        })
    }

This message comes out from the XCode console:

"Succesfully retrieved 103 Saved 103 in local DataStore"

So far so good right? This is my code when I am about to query from the local datastore:

dispatch_async(dispatch_get_main_queue()) { () -> Void in
        let bookQuery = PFQuery(className: "Publication")
            .fromLocalDatastore()
        bookQuery.whereKey("publication_Type", equalTo: "Book")
        bookQuery.findObjectsInBackgroundWithBlock { (bookObject, error) -> Void in

            if error == nil{
                print("Books found: \(bookObject!.count)")
                self.displayData(bookObject!)

            }
        }
    }

And I get from the console: Books found: 0.

What gives? What am I doing wrong? I read and read and read. NOTHING. I thought the ".ignoreACL()" would work but it didn't. Can anyone help me please?

I don't see where you are pinning the PFObject s into the local datastore. Perhaps that is your problem.

Calling any of PFObject s save methods saves them back to your parse server, not a local datastore. Look up how to use pin to accomplish what you want.

Also, dispatching these asynchronous calls to the main queue makes no sense. They are already executing on a background queue. In most cases, you only need to dispatch back to the main queue if you want to do something to the UI and that should be done in the completion handler.

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