为什么我的 NSOperationQueue 运行 在主线程上?

Why is my NSOperationQueue running on main thread?

我已经设置了一个操作队列:

func initialiseOperationQueue(){
    self.operationQueue = NSOperationQueue()
    self.operationQueue.name = "General queue"
    self.operationQueue.maxConcurrentOperationCount = 2

}

然后我在我的队列中添加了一个操作

let op = HPSyncDataOperation(type: HPSyncDataOperationType.OnlineRecord, delegate: self, date: self.latestLastUpdateAt)
self.operationQueue.addOperation(op)

基本上是使用Parse框架异步下载一些在线记录数据。它的实现如下所示:

PFCloud.callFunctionInBackground("recordPosts", withParameters: param, block: { (objects:AnyObject!, error:NSError!) -> Void in
   if error == nil {
       let dataObjects = objects as [PFObject]

       //TROUBLE HERE:
       for object in dataObjects {
            object.pinWithName("Received Posts")
       }
       //abcdefg
   }
})

但是在执行中,当object.pinWithName("Received Posts")是运行时,它会调用

Warning: A long-running operation is being executed on the main thread.

一个操作是否应该 运行 在一个单独的线程上?所以pinWithName,无论同步还是异步,都应该运行在一个单独的线程上吗?

请帮忙!为什么是这样?

您的操作将在后台线程上 运行,但它所做的只是启动另一个异步进程 (PFCloud.callFunctionInBackground),这将启动另一个线程。当其他进程完成时,它会调用主线程上的完成块。

所以,在这种情况下,您的操作和队列基本上什么都不做,实际上您应该获取调用 PFCloud.callFunctionInBackground(即 objects)的结果并在后台处理它线程,如果它可能很耗时。