簡體   English   中英

在Core Data NSPrivateQueueConcurrencyType線程中存儲線程上下文特定的數據

[英]Storing thread context specific data in a Core Data NSPrivateQueueConcurrencyType thread

我需要將線程上下文特定的數據存儲在使用NSPrivateQueueConcurrencyType線程執行的塊中。 這是我想在塊中執行的示例(給出了要存儲的上下文數據的簡化示例,但您明白了):

 dispatch_queue_t myQueue = dispatch_get_current_queue(); NSMutableDictionary *myContext = @{@"Context": @"ContextData"}; dispatch_set_context(myQueue, (__bridge_retained void *)myContext); 

但是問題是不贊成使用用於獲取對當前隊列的引用的函數dispatch_get_current_queue(),因此我不想使用它。 我看不到任何其他用於檢索對此隊列的引用的機制,因此可以針對它保存上下文數據。 我現在想知道蘋果是否已棄用dispatch_get_current_queue(),因為他們不希望人們做我想做的事情-盡管我看不到任何有關此問題的文檔警告。 1.您知道這是一件不好的事嗎? 2.您是否知道任何其他方式來保存與當前線程相關的上下文數據,如果該線程恰巧是系統提供的CoreDataPrivateQueue類型,則所使用的代碼也應該可以工作?

OK,因此可以改為進行NSThread等效調用。 在單個系統生成的,類型為NSPrivateQueueConcurrencyType的MOC上,此方法有效。 使用一段時間后,如果遇到與線程生存期有關的任何問題,它將更新:

[moc performBlockAndWait:^{
    NSThread *currentThread = [NSThread currentThread];
    NSMutableDictionary *myThreadDict = currentThread.threadDictionary;
    [myThreadDict setObject:@"Some thread data" forKey:@"IRThreadData"];
}];
[moc performBlockAndWait:^{
    NSThread *currentThread = [NSThread currentThread];
    NSMutableDictionary *myThreadDict = currentThread.threadDictionary;
    NSLog(@"Retreived dictionary value = %@", [myThreadDict objectForKey:@"IRThreadData"]);
}];

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM