gpt4 book ai didi

ios - iOS核心数据在后台获取导致崩溃

转载 作者:行者123 更新时间:2023-12-01 19:06:03 25 4
gpt4 key购买 nike

在我的应用程序中,我一直在后台进行Core Data工作。
Testflight一直在报告特定的提取操作发生了很多崩溃,该操作从Core Data获取了当前登录的环境对象(我无法复制):

- (Environment *)getActiveEnvironment
{
AppDelegate *ad = [AppDelegate sharedAppDelegate];
NSManagedObjectContext *context = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSConfinementConcurrencyType];
[context setParentContext:ad.managedObjectContext];

NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Environment" inManagedObjectContext:context];
[fetchRequest setEntity:entity];

NSError *error;

//crash points to this line
NSArray *items = [context executeFetchRequest:fetchRequest error:&error];

Environment *tempE = nil;
if(items.count > 0)
{
for(Environment *e in items)
{
if(!e.token && !e.url)
{
break;
}

Environment *mainContextTv = (Environment *)[ad.managedObjectContext objectWithID:e.objectID];
if(e.activeValue)
tempE = mainContextTv;
}
}

return tempE;
}
这是我从Testflight收到的崩溃堆栈:
0   Tower-iSales-Tab    0x002b37b2  testflight_backtrace
1 Tower-iSales-Tab 0x002b2e4a TFSignalHandler
2 libsystem_platform.dylib 0x39144722 _sigtramp
3 CoreData 0x2e1b8cec _perform
4 CoreData 0x2e1c30f4 -[NSManagedObjectContext(_NestedContextSupport) executeRequest:withContext:error:]
5 CoreData 0x2e13477a -[NSManagedObjectContext executeFetchRequest:error:]
6 Tower-iSales-Tab 0x000c7082 -[MySingleton getActiveEnvironment] in MySingleton.m on Line 379
7 Tower-iSales-Tab 0x000c73f8 -[MySingleton getToken] in MySingleton.m on Line 416
8 Tower-iSales-Tab 0x0020c476 -[NetworkManager getPathForViewName:useTimeStamp:timeStamp:index:counter:] in NetworkManager.m on Line 699
9 Tower-iSales-Tab 0x0020aa5a __70-[NetworkManager checkForDataFilesShouldEmptyQueue:isInitialDownload:]_block_invoke167 in NetworkManager.m on Line 415
10 libdispatch.dylib 0x3901b8fa _dispatch_barrier_sync_f_invoke
11 Tower-iSales-Tab 0x0020a33e __70-[NetworkManager checkForDataFilesShouldEmptyQueue:isInitialDownload:]_block_invoke113 in NetworkManager.m on Line 335
12 libdispatch.dylib 0x39017102 _dispatch_call_block_and_release
13 libdispatch.dylib 0x3901c7e4 _dispatch_root_queue_drain
14 libdispatch.dylib 0x3901c9d0 _dispatch_worker_thread2
15 libsystem_pthread.dylib 0x39146dfe _pthread_wqthread
16 libsystem_pthread.dylib 0x39146cc3 start_wqthread
我读了 this answer,它建议将队列类型设置为 NSPrivateQueueConcurrencyType,而不是我可以做的 NSConfinementConcurrencyType,但这不是我可以轻松复制的崩溃,因此我想确定会导致崩溃的原因。
该答案还建议将 performBlockAndWait块用于后台获取,但是在我的函数中我将如何?我需要返回一个 Environment对象,我在整个应用程序中都使用了此函数。
谢谢

最佳答案

据我了解,您不必在这里使用NSConfinementConcurrencyType上下文,因为您仅从主线程调用-getActiveEnvironment。因此,您可以在主上下文(ad.managedObjectContext)上执行所有操作。

然后应仅从主线程访问返回的对象。如果要从其他线程访问它,则必须使用objectWithID:在与该线程关联的上下文中获取它。

关于ios - iOS核心数据在后台获取导致崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19568662/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com