与 Core Data 和 RestKit 无关的关系

发布于 2024-12-12 13:15:46 字数 3653 浏览 0 评论 0原文

我一直在使用 RestKit (0.9.3) 在我的应用程序中填充一些核心数据。实体对象已正确创建,但未创建它们之间的关系。据我所知,我根据以下内容正确设置了内容:

  1. https://github.com /RestKit/RestKit/wiki/Object-mapping
  2. Examples 文件夹中的 RKRelationshipMappingExample

因此,这是我从服务器返回的 JSON 示例:

{
  "artist" : {
    "artistId" : 123,
    "artistName" : "The Artist",
    "albums" : [
      {
        "albumPrice" : 12.99,
        "albumId" : 456,
        "trackCount" : 12,
        "albumName" : "Album Title",
      },
      {
        "albumPrice" : 10.99,
        "albumId" : 789,
        "trackCount" : 10,
        "albumName" : "Another Album Title",
      }
    ]
  }
}

我正在使用 RestKit 填充以下两个托管内容对象:

@interface Artist : NSManagedObject
@property (nonatomic, retain) NSNumber * artistId;
@property (nonatomic, retain) NSString * artistName;
@property (nonatomic, retain) NSSet *albums;
@end
@interface Artist (CoreDataGeneratedAccessors)
- (void)addAlbumsObject:(Album *)value;
- (void)removeAlbumsObject:(Album *)value;
- (void)addAlbums:(NSSet *)values;
- (void)removeAlbums:(NSSet *)values;
@end

@interface Album : NSManagedObject
@property (nonatomic, retain) NSNumber * albumId;
@property (nonatomic, retain) NSString * albumName;
@property (nonatomic, retain) NSNumber * albumPrice;
@property (nonatomic, retain) NSNumber * trackCount;
@property (nonatomic, retain) NSManagedObject *artist;
@end

我使用 RestKit 设置了以下配置,目的是创建 3 个核心数据项:一位艺术家和两张专辑。艺术家实体具有“专辑”关系,应该是这两张专辑的集合。

// Initialize our RestKit singleton
RKClient *client = [RKClient clientWithBaseURL:API_URL];  

// Initialize for Object mapping
RKObjectManager* objectManager = [RKObjectManager objectManagerWithBaseURL:API_URL];
objectManager.objectStore = [RKManagedObjectStore objectStoreWithStoreFilename:@"AppName.sqlite"];

// Configure ALBUM mapping
RKManagedObjectMapping* albumMapping = [RKManagedObjectMapping mappingForClass:[Album class]];
albumMapping.primaryKeyAttribute = @"albumId";
[albumMapping mapKeyPath:@"albumId" toAttribute:@"albumId"];
[albumMapping mapKeyPath:@"albumName" toAttribute:@"albumName"];
[albumMapping mapKeyPath:@"albumPrice" toAttribute:@"albumPrice"];
[albumMapping mapKeyPath:@"trackCount" toAttribute:@"trackCount"];

// Configure ARTIST mapping
RKManagedObjectMapping* artistMapping = [RKManagedObjectMapping mappingForClass:[Artist class]];
artistMapping.primaryKeyAttribute = @"artistId";
[artistMapping mapKeyPath:@"artistId" toAttribute:@"artistId"];
[artistMapping mapKeyPath:@"artistName" toAttribute:@"artistName"];    
[artistMapping mapKeyPath:@"albums" toRelationship:@"albums" withMapping:albumMapping];

// Register mappings with the provider
[[RKObjectManager sharedManager].mappingProvider setMapping:albumMapping forKeyPath:@"albums"];
[[RKObjectManager sharedManager].mappingProvider setMapping:artistMapping forKeyPath:@"artist"];

然后在我的程序中,我加载数据:

RKObjectManager *objectManager = [RKObjectManager sharedManager];
[objectManager loadObjectsAtResourcePath:@"/artist/123/albums" delegate:self];

执行此操作后,我的核心数据存储将填充三个对象(一位艺术家和两张专辑),但不会创建它们之间的关系。

结果是我可以直接获取专辑列表,或者我可以直接获取艺术家。但如果我尝试使用:artist.albums 结果是空的。

谁能就我可能做错的事情提供建议,或者如何进一步解决这个问题?

非常感谢。

编辑:更新 10/26

我刚刚使用这些相同的配置设置了一个小示例项目,并且所有内容都已正确加载(即,艺术家实体是根据与其专辑的关系创建的)。遗憾的是,这意味着问题出在我的更大项目中,而不是我之前概述的内容中。话虽如此,我将继续艰难地进行更新,因此,如果有人对如何解决此问题或特别在哪里查找有建议,我将不胜感激。

编辑:已解决

令人尴尬的是,问题只是我使用了错误的基本网址。明天我将发布完整的详细信息(以防它可以帮助其他人)。

I've been working with RestKit (0.9.3) to populate some Core Data in my app. The entity objects are created correctly, but the relationships between them are not created. From what I can tell, I have things setup correctly according to:

  1. https://github.com/RestKit/RestKit/wiki/Object-mapping
  2. RKRelationshipMappingExample in the Examples folder

So, here's a sample of the JSON that I'm getting back from the server:

{
  "artist" : {
    "artistId" : 123,
    "artistName" : "The Artist",
    "albums" : [
      {
        "albumPrice" : 12.99,
        "albumId" : 456,
        "trackCount" : 12,
        "albumName" : "Album Title",
      },
      {
        "albumPrice" : 10.99,
        "albumId" : 789,
        "trackCount" : 10,
        "albumName" : "Another Album Title",
      }
    ]
  }
}

And I am using RestKit to populate the following two managed objects:

@interface Artist : NSManagedObject
@property (nonatomic, retain) NSNumber * artistId;
@property (nonatomic, retain) NSString * artistName;
@property (nonatomic, retain) NSSet *albums;
@end
@interface Artist (CoreDataGeneratedAccessors)
- (void)addAlbumsObject:(Album *)value;
- (void)removeAlbumsObject:(Album *)value;
- (void)addAlbums:(NSSet *)values;
- (void)removeAlbums:(NSSet *)values;
@end

@interface Album : NSManagedObject
@property (nonatomic, retain) NSNumber * albumId;
@property (nonatomic, retain) NSString * albumName;
@property (nonatomic, retain) NSNumber * albumPrice;
@property (nonatomic, retain) NSNumber * trackCount;
@property (nonatomic, retain) NSManagedObject *artist;
@end

I've setup the following configurations with RestKit, and the intent is for 3 Core Data items to be created: one artist and two albums. The artist entity has a "albums" relationship which should be a set of these two albums.

// Initialize our RestKit singleton
RKClient *client = [RKClient clientWithBaseURL:API_URL];  

// Initialize for Object mapping
RKObjectManager* objectManager = [RKObjectManager objectManagerWithBaseURL:API_URL];
objectManager.objectStore = [RKManagedObjectStore objectStoreWithStoreFilename:@"AppName.sqlite"];

// Configure ALBUM mapping
RKManagedObjectMapping* albumMapping = [RKManagedObjectMapping mappingForClass:[Album class]];
albumMapping.primaryKeyAttribute = @"albumId";
[albumMapping mapKeyPath:@"albumId" toAttribute:@"albumId"];
[albumMapping mapKeyPath:@"albumName" toAttribute:@"albumName"];
[albumMapping mapKeyPath:@"albumPrice" toAttribute:@"albumPrice"];
[albumMapping mapKeyPath:@"trackCount" toAttribute:@"trackCount"];

// Configure ARTIST mapping
RKManagedObjectMapping* artistMapping = [RKManagedObjectMapping mappingForClass:[Artist class]];
artistMapping.primaryKeyAttribute = @"artistId";
[artistMapping mapKeyPath:@"artistId" toAttribute:@"artistId"];
[artistMapping mapKeyPath:@"artistName" toAttribute:@"artistName"];    
[artistMapping mapKeyPath:@"albums" toRelationship:@"albums" withMapping:albumMapping];

// Register mappings with the provider
[[RKObjectManager sharedManager].mappingProvider setMapping:albumMapping forKeyPath:@"albums"];
[[RKObjectManager sharedManager].mappingProvider setMapping:artistMapping forKeyPath:@"artist"];

And then later in my program, I load the data:

RKObjectManager *objectManager = [RKObjectManager sharedManager];
[objectManager loadObjectsAtResourcePath:@"/artist/123/albums" delegate:self];

After this is executed, my Core Data store is populated with the three objects (one artist and two albums), but the relationship between them is not created.

The result is that I can fetch this list of albums directly, or I can fetch the artist directly. But if I try to use: artist.albums the result is empty.

Can anyone offer suggestions on what I could be doing wrong, or how to further troubleshoot this?

Thanks so much.

EDIT: Update 10/26

I just setup a small sample project with these same configurations and everything was loaded correctly (i.e. The artist entity was created with a relationship to it's albums). Sadly, this means that the issue is something in my larger project, rather than in what I outlined here earlier. With that said, I'll continue trudging though and updating, so if anyone has advice on how to troubleshoot this or where in particular to look, I would greatly appreciate it.

EDIT: Resolved

Embarrassingly, the problem was simply that I was using the wrong base url. I'll post full details (in case it can help anyone else) tomorrow.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

埖埖迣鎅 2024-12-19 13:15:46

嗯...我们有一个解决方案。我很尴尬地说,问题只是我在我的主项目中使用了错误的基本网址。我对 API 本地副本返回的 JSON 进行了一些更新,但基本 url 仍然指向在线 API 副本。

我所做的更改是以这种方式格式化 JSON,而不是让“艺术家”和“专辑”成为 JSON 中的单独实体。

Well... we have a resolution. I'm embarrassed to say that the problem was simply that I was using the wrong base url in my main project. I made some updates to the JSON returned by the local copy of the API, but the base url was still pointing to the copy of the API online.

The change I made was to format the JSON this way rather than having "artist" and "albums" be separate entities in the JSON.

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