print("queue1: \(NSOperationQueue.currentQueue())")
managedObjectContext.performBlockAndWait({
print("queue2: \(NSOperationQueue.currentQueue())")
})
and output is:
和输出是:
queue1: Optional(<NSOperationQueue: 0x7fa31c030cf0>{name = 'NSOperationQueue 0x7fa31c030cf0'})
queue2: Optional(<NSOperationQueue: 0x7fa319d114d0>{name = 'NSOperationQueue Main Queue'})
So, performBlockAndWait
runs the block in main thread. Don't know why? Is this the expected behaviour?
因此,performBlockAndWait在主线程中运行块。不知道为什么?这是预期的行为吗?
So, any solutions for the problems:
那么,任何问题的解决方案:
- I need to run the code of the block in background, else it freezes UI. So, how can I?, or
- If I don't use
performBlockAndWait
the code in the block crash with the descriptionTerminating app due to uncaught exception NSInvalidArgumentException, reason _referenceData64 only defined for abstract class
. Any alternative?
我需要在后台运行块的代码,否则它会冻结UI。那么,我怎么样?或者
如果我不使用performBlockAndWait块中的代码崩溃与描述终止应用程序由于未捕获的异常NSInvalidArgumentException,原因_referenceData64仅为抽象类定义。还有其他选择
2 个解决方案
#1
1
PerformBlock() and performBlockAndWait() runs on the queue in which your context is created. So if your managedObjectContext is created in main queue it will run in the main queue.
PerformBlock()和performBlockAndWait()在创建上下文的队列上运行。因此,如果您的managedObjectContext是在主队列中创建的,它将在主队列中运行。
If you want background execution you need to create a background context :
如果要执行后台,则需要创建后台上下文:
let privateContext = NSManagedObjectContext(
concurrencyType: .PrivateQueueConcurrencyType)
privateContext.persistentStoreCoordinator = mainQueueContext.persistentStoreCoordinator
privateContext.performBlock {
...
}
#2
0
Perform your task you want to do in background in a separate function with a completion handler.
使用完成处理程序在单独的函数中执行要在后台执行的任务。
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)) {
func performFuncWithCompletion() { (Success) in
if (Success)
NSOperationQueue.mainQueue().addOperationWithBlock {
// update UI here
}
else {
// Show error message?
}
}
}
func performFuncWithCompletion(completion : (SuccessStatus: Bool)) {
// perform your back ground function here.
// Decide if everything went fine
if everythingWentFine {
completion(true)
}
else {
completion(false)
}
}
Ask if any questions
询问是否有任何问题
#1
1
PerformBlock() and performBlockAndWait() runs on the queue in which your context is created. So if your managedObjectContext is created in main queue it will run in the main queue.
PerformBlock()和performBlockAndWait()在创建上下文的队列上运行。因此,如果您的managedObjectContext是在主队列中创建的,它将在主队列中运行。
If you want background execution you need to create a background context :
如果要执行后台,则需要创建后台上下文:
let privateContext = NSManagedObjectContext(
concurrencyType: .PrivateQueueConcurrencyType)
privateContext.persistentStoreCoordinator = mainQueueContext.persistentStoreCoordinator
privateContext.performBlock {
...
}
#2
0
Perform your task you want to do in background in a separate function with a completion handler.
使用完成处理程序在单独的函数中执行要在后台执行的任务。
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)) {
func performFuncWithCompletion() { (Success) in
if (Success)
NSOperationQueue.mainQueue().addOperationWithBlock {
// update UI here
}
else {
// Show error message?
}
}
}
func performFuncWithCompletion(completion : (SuccessStatus: Bool)) {
// perform your back ground function here.
// Decide if everything went fine
if everythingWentFine {
completion(true)
}
else {
completion(false)
}
}
Ask if any questions
询问是否有任何问题