Reputation: 3417
I have created simple relational database (like exists in other dbs) model in the iOS Realm DB. Where i created 2 separate Realm models (tables) user ids with other details and a Wishlist, where the user has an array of Wishlist using RLMArray
. Wishlist can contain 1 or more products as in my previous question. In this model table with user ids is constant but Wishlist table of RLMArray
can be updated or deleted. I am able to update RLMArray
using realm addOrUpdateObject
but How can i delete elements from RLMArray
?
Example relational database model is below :
Code is similar to this :
#import <Realm/Realm.h>
@class User;
// User model
@interface User : RLMObject
@property NSString *name;
@property NSString *user_id;
@property RLMArray< Wishlist *>< Wishlist > *wishlist;
@end
// Wishlist model
@interface Wishlist : RLMObject
@property NSString *id;
@property NSInteger *activity;
@property NSInteger *cost;
@end
RLM_ARRAY_TYPE(Wishlist) // define RLMArray<Person>
// Implementations
@implementation User
@end // none needed
@implementation Wishlist
@end // none needed
Read data from realm :
RLMResults *watchlistDB = [Watchlist allObjects];
WatchlistDB = [realm_data objectAtIndex:index];
RLMArray *realm_array = WatchlistDB.watchlist;
Insert New activity:
User *user_realm = [[MyWatchlistDB alloc] init];
user_realm.user_id = _user_id;
user_realm.name = _user_name;
for (NSDictionary *tempDict in activity)
{
Wishlist *wishlist_realm = [[Wishlist alloc] init];
newWatchlist.id =[tempDict objectForKey:@"id"];
newWatchlist.activity =[tempDict objectForKey:@"activity"];
newWatchlist.cost = [tempDict objectForKey:@"cost"];
[user_realm.wishlist addObject:newWatchlist];
}
RLMRealm *realm = [RLMRealm defaultRealm];
[realm beginWriteTransaction];
[realm addOrUpdateObject:user_realm];
[realm commitWriteTransaction];
How can i delete elements from RLMArray ? to update user wishlist when he delete some activities ?
Upvotes: 1
Views: 2115
Reputation: 15991
Realm is an object database, not a relational database, so you shouldn't need to implement a foreign key system in order to link objects. If you want to see which User
a WishList
item belongs, you can use the RLMLinkingObject
feature to provide a reverse-lookup instead of what you're doing now.
// Wishlist model
@interface Wishlist : RLMObject
@property NSInteger *activity;
@property NSInteger *cost;
@property (readonly) RLMLinkingObjects *users;
@end
RLM_ARRAY_TYPE(Wishlist) // define RLMArray<Person>
// Implementations
@implementation Wishlist
+ (NSDictionary *)linkingObjectsProperties {
return @{
@"users": [RLMPropertyDescriptor descriptorWithClass:User.class
propertyName:@"wishlist"]
};
}
@end
This means you can call wishlist.users.firstObject
to get the user in which that Wishlist
belongs, without needing to do a lot of manual linking work.
Additionally, if you've got a Realm property in User
that you want to use to uniquely identify objects, you can explicitly mark that one as a primary key which lets you query for specific objects (Even though we call it a primary key for convinience, it's still not the same as a relational database).
// User model
@interface User : RLMObject
@property NSString *name;
@property NSString *user_id;
@property RLMArray< Wishlist *>< Wishlist > *wishlist;
@end
@implementation User
+ (NSString *)primaryKey {
return @"user_id";
}
@end
User
and Wishlist
objects are stored in separate tables in Realm, with neither explicitly owning each other (They're just making references). If a Wishlist
item is unique to just one User
, then you can just delete the Wishlist
object from Realm and it will get removed from the RLMArray
as well.
To delete all of the wishlist entries belonging to a single user, you could simply do this.
//Get John Smith's entry
User *johnSmith = [User objectForPrimaryKey:@"084"];
//Delete all of the items in John's wishlist
RLMRealm *realm = [RLMRealm defaultRealm]
[realm transactionWithBlock:^{
[realm deleteObjects:johnSmith.wishlist];
}];
Upvotes: 3
Reputation: 3417
Some how i manage to delete data from realm. Using RLMArray
and addOrUpdateObject
.
RLMArray<Wishlist> *wishlist_array = Userdb_realm.wishlist; // reading existing data
User *Userdb = [[User alloc] init];
Userdb.user_id = _Userdb_id;
Userdb.name = _Userdb_name;
for (NSString *temp in delArray)
{
for (int i = 0; i<[wishlist_array count]; i++) {
Wishlist *wishlistTodel = wishlist_array[i];
if ([wishlistTodel.user_id isEqualToString:temp]) {
RLMRealm *realm = [RLMRealm defaultRealm];
[realm beginWriteTransaction];
[wishlist_array removeObjectAtIndex:i];
[realm commitWriteTransaction];
break;
}
}
}
Userdb.wishlist = wishlist_array;
RLMRealm *realm = [RLMRealm defaultRealm];
[realm beginWriteTransaction];
[realm addOrUpdateObject:Userdb];
[realm commitWriteTransaction];
If anyone have better solution can answer here. Thank you for all your response.
Upvotes: 1