Closed yuldong closed 8 years ago
First of all you should be aware that batching writes to larger transactions is the recommended way to go with Realm. There are different possibilities to achieve that. It could look like below:
RLMRealm *realm = [RLMRealm defaultRealm];
NSMutableArray *products = [NSMutableArray new];
for (NSDictionary *productData in …) {
YYYProductInfo *product = …;
[productArray addObject:product];
}
[realm transactionWithBlock:^{
[realm addObjects:products];
}];
Alternatively you could also insert them one-by-one in a larger transaction:
RLMRealm *realm = [RLMRealm defaultRealm];
[realm transactionWithBlock:^{
for (NSDictionary *productData in …) {
YYYProductInfo *product = …;
[realm addObject:product];
}
}];
Does this answer your question?
@mrackwitz thanks a lot, i just focus on the class of my own, and forget that RLMRealm also has method to do such thing. thank you very much.
define a class:
import <Realm/Realm.h>
@interface YYYProductInfo : RLMObject @property NSString name; @property NSInteger price; @property NSData productDescription; @property NSDate *date; @end RLM_ARRAY_TYPE(YYYProductInfo) then, we could insert a product to a realm like this:
If I want to insert many products once a time, how could i do?