开发者

Get all objects that belongs to a entity in a one-to-many relationship with core data

I'm working on simple iPhone app and I'm trying to get core data working. I currently have two entities in my data model, it looks like this:

Get all objects that belongs to a entity in a one-to-many relationship with core data

In my application I 开发者_JS百科have a tableview that should display all pages that belongs to a certain note block. I currently use:

NoteblockAppController * appController = [NoteblockAppController sharedNoteblockAppController];
NSArray * list = [appController allInstancesOf:@"Page" orderBy:@"createdAt"];
noteblockPages = [list mutableCopy];

But this obviously doesn't work since it always shows the same pages. I assume that I have to fetch the result some other way, but I don't know how.

I'm quite stuck so any tips/tricks would be great.

Thanks.


I like to add derived methods to access things like this. I add these to the custom class but don't model them in the data model.

Once you get your accessor working correctly, it should return a mutable set. Then:

- (NSArray *)noteblockPagesArray {
  NSMutableSet *pages = self.noteblockPages;
  NSSortDescriptor *sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"createdAt" ascending:YES];

  NSArray *orderedPages = [pages sortedArrayUsingDescriptors:[NSArray arrayWithObject:sortDescriptor]];
  return orderedPages;
}

Modify to cache value as appropriate/needed.


Let CoreData do the work. You have an instance of the Noteblock, correct?

[yourNoteblock noteblockPages]
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜