Restkit 对象映射两个类访问错误
我正在使用 RestKit 映射两类对象。当我自己做其中任何一个时,效果都很好。但是当我一起调用它们时,它会在 RKObjectMappingOperation.m
中的 449
行崩溃,
[destinationSet setSet:destinationObject];
并出现 "EXC_BAD_ACCESS"
错误。
这是我的两种映射方法:
- (RKObjectLoader *)saves
{
// Create an object manager and connect core data's persistent store to it
RKObjectManager *objectManager = [RKObjectManager sharedManager];
RKManagedObjectStore* objectStore = [RKManagedObjectStore objectStoreWithStoreFilename:@"Db.sqlite"];
objectManager.objectStore = objectStore;
// Define our author mapping for saved places
RKManagedObjectMapping *authorMapping = [RKManagedObjectMapping mappingForEntityWithName:@"Person"];
[authorMapping mapAttributes:@"uid", nil];
// Define our place mapping
RKManagedObjectMapping *placeMapping = [RKManagedObjectMapping mappingForEntityWithName:@"Place"];
[placeMapping mapAttributes:@"uid",@"name",@"address", nil];
// Now, connect the two via a save
RKManagedObjectMapping *saveMapping = [RKManagedObjectMapping mappingForEntityWithName:@"Save"];
[saveMapping mapAttributes:@"uid", @"timestamp", nil];
[saveMapping mapKeyPath:@"place" toRelationship:@"place" withMapping:placeMapping];
[saveMapping mapKeyPath:@"author" toRelationship:@"author" withMapping:authorMapping];
// We expect to find the place entity inside of a dictionary keyed "saves"
[objectManager.mappingProvider setMapping:saveMapping forKeyPath:@"saves"];
// Prepare our object loader to load and map objects from remote server, and send
RKObjectLoader *objectLoader = [objectManager objectLoaderWithResourcePath:@"places/saves" delegate:self];
objectLoader.method = RKRequestMethodGET;
[objectLoader send];
return objectLoader;
}
- (RKObjectLoader *)recommends
{
// Create an object manager and connect core data's persistent store to it
RKObjectManager *objectManager = [RKObjectManager sharedManager];
RKManagedObjectStore* objectStore = [RKManagedObjectStore objectStoreWithStoreFilename:@"Db.sqlite"];
objectManager.objectStore = objectStore;
// Define our author mapping for recommended places
RKManagedObjectMapping *authorMapping = [RKManagedObjectMapping mappingForEntityWithName:@"Person"];
[authorMapping mapAttributes:@"uid", nil];
// Define our place mapping
RKManagedObjectMapping *placeMapping = [RKManagedObjectMapping mappingForEntityWithName:@"Place"];
[placeMapping mapAttributes:@"uid",@"name",@"address", nil];
// Now, connect the two via a recommend
RKManagedObjectMapping *recommendMapping = [RKManagedObjectMapping mappingForEntityWithName:@"Recommend"];
[recommendMapping mapAttributes:@"uid", @"timestamp", nil];
[recommendMapping mapKeyPath:@"place" toRelationship:@"place" withMapping:placeMapping];
[recommendMapping mapKeyPath:@"author" toRelationship:@"author" withMapping:authorMapping];
// We expect to find the place entity inside of a dictionary keyed "recommends"
[objectManager.mappingProvider setMapping:recommendMapping forKeyPath:@"recommends"];
// Prepare our object loader to load and map objects from remote server, and send
RKObjectLoader *objectLoader = [objectManager objectLoaderWithResourcePath:@"places/recommends" delegate:self];
objectLoader.method = RKRequestMethodGET;
[objectLoader send];
return objectLoader;
}
当我调用其中一种或另一种时,它就会起作用。当我调用两者时,
[self saves];
[self recommends];
它崩溃了。知道为什么吗?
I am mapping two classes of objects with RestKit. When I do either of them by themselves, it works out perfectly fine. But when I call them together, it will crash on line 449
in RKObjectMappingOperation.m
,
[destinationSet setSet:destinationObject];
With an "EXC_BAD_ACCESS"
error.
Here are my two mapping methods:
- (RKObjectLoader *)saves
{
// Create an object manager and connect core data's persistent store to it
RKObjectManager *objectManager = [RKObjectManager sharedManager];
RKManagedObjectStore* objectStore = [RKManagedObjectStore objectStoreWithStoreFilename:@"Db.sqlite"];
objectManager.objectStore = objectStore;
// Define our author mapping for saved places
RKManagedObjectMapping *authorMapping = [RKManagedObjectMapping mappingForEntityWithName:@"Person"];
[authorMapping mapAttributes:@"uid", nil];
// Define our place mapping
RKManagedObjectMapping *placeMapping = [RKManagedObjectMapping mappingForEntityWithName:@"Place"];
[placeMapping mapAttributes:@"uid",@"name",@"address", nil];
// Now, connect the two via a save
RKManagedObjectMapping *saveMapping = [RKManagedObjectMapping mappingForEntityWithName:@"Save"];
[saveMapping mapAttributes:@"uid", @"timestamp", nil];
[saveMapping mapKeyPath:@"place" toRelationship:@"place" withMapping:placeMapping];
[saveMapping mapKeyPath:@"author" toRelationship:@"author" withMapping:authorMapping];
// We expect to find the place entity inside of a dictionary keyed "saves"
[objectManager.mappingProvider setMapping:saveMapping forKeyPath:@"saves"];
// Prepare our object loader to load and map objects from remote server, and send
RKObjectLoader *objectLoader = [objectManager objectLoaderWithResourcePath:@"places/saves" delegate:self];
objectLoader.method = RKRequestMethodGET;
[objectLoader send];
return objectLoader;
}
- (RKObjectLoader *)recommends
{
// Create an object manager and connect core data's persistent store to it
RKObjectManager *objectManager = [RKObjectManager sharedManager];
RKManagedObjectStore* objectStore = [RKManagedObjectStore objectStoreWithStoreFilename:@"Db.sqlite"];
objectManager.objectStore = objectStore;
// Define our author mapping for recommended places
RKManagedObjectMapping *authorMapping = [RKManagedObjectMapping mappingForEntityWithName:@"Person"];
[authorMapping mapAttributes:@"uid", nil];
// Define our place mapping
RKManagedObjectMapping *placeMapping = [RKManagedObjectMapping mappingForEntityWithName:@"Place"];
[placeMapping mapAttributes:@"uid",@"name",@"address", nil];
// Now, connect the two via a recommend
RKManagedObjectMapping *recommendMapping = [RKManagedObjectMapping mappingForEntityWithName:@"Recommend"];
[recommendMapping mapAttributes:@"uid", @"timestamp", nil];
[recommendMapping mapKeyPath:@"place" toRelationship:@"place" withMapping:placeMapping];
[recommendMapping mapKeyPath:@"author" toRelationship:@"author" withMapping:authorMapping];
// We expect to find the place entity inside of a dictionary keyed "recommends"
[objectManager.mappingProvider setMapping:recommendMapping forKeyPath:@"recommends"];
// Prepare our object loader to load and map objects from remote server, and send
RKObjectLoader *objectLoader = [objectManager objectLoaderWithResourcePath:@"places/recommends" delegate:self];
objectLoader.method = RKRequestMethodGET;
[objectLoader send];
return objectLoader;
}
When I call one, or the other, it works. When I call both,
[self saves];
[self recommends];
It crashes. Any idea why?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
对 set
objectManager.objectStore
的调用导致之前设置的objectStore
从objectManager
中释放。第一次调用[self saves]
创建并设置一个objectStore
对象,然后第二次调用[self suggests]
重复此操作,从而删除第一个一组在[自我保存]
中。在[self saves]
开始处理期间的某个时间,原始(已释放)objectStore
对象正在被访问,因此崩溃。一个潜在的修复方法是将 objectStore setter 重构为一个单独的方法,该方法由两个方法调用,或者将其包装在 if (!objectManager.objectStore) { ... }< /代码> 声明。
The call to set
objectManager.objectStore
is causing the previously setobjectStore
to be released fromobjectManager
. The first call,[self saves]
creates and sets anobjectStore
object, then second call[self recommends]
repeats this, thereby removing the first one set in[self saves]
. Some time during the processing started off by[self saves]
, the original (released)objectStore
object is being accessed, hence the crash.A potential fix would be to refactor out the
objectStore
setter into a separate method which is called by both methods, or wrap it in anif (!objectManager.objectStore) { ... }
statement.