Restkit 对象映射两个类访问错误

发布于 2025-01-06 13:18:41 字数 3606 浏览 0 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

只为守护你 2025-01-13 13:18:41

对 set objectManager.objectStore 的调用导致之前设置的 objectStoreobjectManager 中释放。第一次调用 [self saves] 创建并设置一个 objectStore 对象,然后第二次调用 [self suggests] 重复此操作,从而删除第一个一组在[自我保存]中。在[self saves]开始处理期间的某个时间,原始(已释放)objectStore对象正在被访问,因此崩溃。

一个潜在的修复方法是将 objectStore setter 重构为一个单独的方法,该方法由两个方法调用,或者将其包装在 if (!objectManager.objectStore) { ... }< /代码> 声明。

The call to set objectManager.objectStore is causing the previously set objectStore to be released from objectManager. The first call, [self saves] creates and sets an objectStore 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 an if (!objectManager.objectStore) { ... } statement.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文