I have a lengthy piece of code which downloads data from Parse. This is the shortened version:
func runQuery(query:PFQuery) {
var backgroundQueue = NSOperationQueue()
backgroundQueue.addOperationWithBlock(){
query.findObjectsInBackgroundWithBlock {
(objects: [AnyObject]?, error: NSError?) -> Void in
if error == nil {
// ... some stuff here...
// the following line produces the parse warning.
// file is a PFFile object.
if let imageData = file.getData() {
// ...
}
// even more stuff here ...
}
}
I know that PFFile.getData() blocks the running thread, but this is ok, because it runs in a NSOperationQueue. So from my thinking, I'm processing in the background. And it works, when I call runQuery(), it immediately returns and the UI works. But why on earth do I get this warning and how can I get rid of it? I don't want to change the PFFile.GetData() call to PFFile.getDataInBackgroundWithBlock(), because that would require significant code changes. And I see not need for that.
NSOperationQueue () will return the main operation queue, which is running on the main thread. Not on a background thread.
Meanwhile, I found the problem.
query.findObjectsInBackgroundWithBlock { ... }
puts the code inside the block back on the main queue even if you are executing it on a background thread! I don't know if that is intended.
Knowing that, it is easy to solve the problem: Bracket the time-consuming download code in a NSOperationQueue(), which runs on a background thread. Then you can use the synchronous parse methods, which makes the code a lot easier to write and read. All warnings are gone.
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.