Reputation: 571
I'm just learning to programming for ios. Need advice, I read the json response, like this:
[{
"id": 1,
"title": "Book1",
"author": "BookAuthor1",
"chapters": [
"Chapter1",
"Chapter2",
"Chapter3",
"Chapter4",
"Chapter5"
]
},
{
"id": 2,
"title": "Book2",
"author": "BookAuthor2",
"chapters": [
"Mychapter1",
"Mychapter2",
"Mychapter3",
"Mychapter4",
"Mychapter5"
]
}]
I made a model for the realm:
//Book.h
@interface Book : RLMObject
@property NSInteger bookID;
@property NSString *title;
@property NSString *author;
@property RLMArray<Chapter> *chapters;
@end
RLM_ARRAY_TYPE(Book)
//Book.m
@implementation Book
+ (NSString *)primaryKey {
return @"bookID";
}
@end
//Chapter.h
@interface Chapter : RLMObject
@property NSString *chapterTitle;
@end
RLM_ARRAY_TYPE(Chapter)
//Chapter.m
@implementation Chapter
@end
How to adjust the model to the upgrade, the data is updated in both tables? Now when I do
[Book createOrUpdateInRealm: defaultRealm withValue: newBook];
occurs only pack now offers Book table, and in Chapter new table and added to the new data (duplicate data). In the array of Chapters, only the name of Chapter, no id, to use for primaryKey. What in such cases can be done?
Upvotes: 1
Views: 98
Reputation: 1286
Because you have no primary key for Chapter
. Realm
doesn't know what to update. So Realm
just do its job by adding new Chapter
into the table.
I would suggest you to add primary key for Chapter
as well.
Upvotes: 2