gpt4 book ai didi

ios - RestKit:在后台执行 RKMapperOperation 时崩溃

转载 作者:行者123 更新时间:2023-11-29 03:24:51 24 4
gpt4 key购买 nike

我试图通过推送到主线程中完成的后台 Core Data/RestKit 操作来加速应用程序。以下简单的映射操作在 RestKit 代码中崩溃:

RKObjectManager *manager = [RKObjectManager sharedManager];
NSPredicate *pre = [NSPredicate predicateWithFormat:@"keyPath IN %@", objectType];
RKEntityMapping *mapping = (RKEntityMapping *)[[[manager responseDescriptors] filteredArrayUsingPredicate:pre].firstObject mapping];
NSDictionary *mappingsDictionary = @{ [NSNull null]: mapping };


RKManagedObjectStore *store = [RKManagedObjectStore defaultStore];
RKManagedObjectMappingOperationDataSource *mappingDataSource = [[RKManagedObjectMappingOperationDataSource alloc] initWithManagedObjectContext:store.mainQueueManagedObjectContext cache:store.managedObjectCache];


RKMapperOperation *mapperOperation = [[RKMapperOperation alloc] initWithRepresentation:data mappingsDictionary:mappingsDictionary];
mapperOperation.mappingOperationDataSource = mappingDataSource;


[manager.operationQueue addOperation:mapperOperation];

如果我在主线程中执行操作:

[mapperOperation execute:&mappingError];

一切正常。

我应该补充一下,只有当映射涉及关系时才会发生崩溃。我的意思是,只有当我获得的 JSON 包含子对象时,它才会崩溃。

知道我做错了什么吗?

更新:更多详细信息。

范围内的模型:

用户照片用户“有很多”照片

映射:

RKEntityMapping *entityMappingUser = [RKEntityMapping mappingForEntityForName:@"User" inManagedObjectStore:managedObjectStore];
[entityMappingUser addAttributeMappingsFromDictionary:@{
@"id": @"userID",
@"name": @"name",
@"surname": @"surname",
}];
entityMappingUser.identificationAttributes = @[ @"userID" ];


RKEntityMapping *entityMappingPhoto = [RKEntityMapping mappingForEntityForName:@"Photo" inManagedObjectStore:managedObjectStore];
[entityMappingPhoto addAttributeMappingsFromDictionary:@{
@"id": @"photoID",
@"uri": @"uri",
}];
entityMappingPhoto.identificationAttributes = @[ @"photoID" ];


[entityMappingUser addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"photos" toKeyPath:@"photos" withMapping:entityMappingPhoto]];

JSON:

"users":{"245659":{"name":"john","surname":"smith", "id":245659, "photos":[{"id":28945,"uri":"http://..."}]}}

这就是我正在做的事情:

  • 当我获得 JSON 时,我会使用问题开头的代码异步映射用户
  • 我通过 NSFetchedResultsController 在 collectionView 中显示这些用户

我在不同的地方遇到随机崩溃。例如,我有时会在 RKMappingOperation, l.575 (v20.3) 中得到一个:

if (attributeMapping.destinationKeyPath) {
[self.destinationObject setValue:value forKeyPath:attributeMapping.destinationKeyPath]; #EXC_BAD_ACCESS
}

然而,destinationObject 看起来“不错”

<TMUser: 0xb54ad20> (entity: User; id: 0xb545900 <x-coredata:///User/tA94DE417-D68E-4D94-927F-6E2667A3BCFD464> ; data: {
surname = "smith";
name =nil;
photos = (
);
})

value(“john”)和destinationKeyPath(名称)也是如此。

最佳答案

您不应在不同于创建托管对象上下文的线程的后台线程上执行这些操作。这将导致 CoreData 存储不稳定,并导致应用程序崩溃。您必须非常小心如何使用线程访问对象。例如,访问从不属于当前线程的上下文中检索到的对象将导致崩溃。

请务必阅读 Apple 提供的 Concurrency with CoreData 文档: https://developer.apple.com/library/ios/documentation/cocoa/conceptual/CoreData/Articles/cdConcurrency.html

关于ios - RestKit:在后台执行 RKMapperOperation 时崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20586155/

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