-
-
Save Daivest/3b65f0f132fd69e3237e773eb2381df1 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import <Realm/Realm.h> | |
#import "Member.h" | |
@interface Chat : RLMObject | |
@property NSString *Id; | |
@property NSString *info; | |
@property RLMArray<Member *> <Member> *members; | |
@end | |
@implementation Chat | |
+ (NSString *)primaryKey { | |
return @"Id"; | |
} | |
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import <Realm/Realm.h> | |
@class User; | |
@interface Member : RLMObject | |
@property NSString *Id; | |
@property NSString *info; | |
@property User *user; | |
@end | |
RLM_ARRAY_TYPE(Member) | |
@implementation Member | |
+ (NSString *)primaryKey { | |
return @"Id"; | |
} | |
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import <Realm/Realm.h> | |
@interface User : RLMObject | |
@property NSString *Id; | |
@property NSString *name; | |
@property NSString *status; | |
@end | |
@implementation User | |
+ (NSString *)primaryKey { | |
return @"Id"; | |
} | |
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import "ViewController.h" | |
#import <Realm/Realm.h> | |
#import "User.h" | |
#import "Member.h" | |
#import "Chat.h" | |
@interface ViewController () | |
@property Chat *chat; | |
@property RLMResults *list; | |
@property RLMNotificationToken *tokenChat; | |
@property RLMNotificationToken *tokenList; | |
@property RLMRealm *realm; | |
@end | |
@implementation ViewController | |
- (void)viewDidLoad { | |
[super viewDidLoad]; | |
_realm = [RLMRealm defaultRealm]; | |
[self _generateChat]; | |
[self _configureResults]; | |
[self _configureListResults]; | |
} | |
#pragma mark - Actions | |
- (IBAction)changeChat:(id)sender { | |
[_realm beginWriteTransaction]; | |
Chat *obj = [Chat objectInRealm:_realm forPrimaryKey:@"first"]; | |
obj.info = [NSUUID UUID].UUIDString; | |
[_realm addOrUpdateObjects:@[obj]]; | |
[_realm commitWriteTransaction]; | |
} | |
- (IBAction)changeMember:(id)sender { | |
[_realm beginWriteTransaction]; | |
Member *member = [Member objectInRealm:_realm forPrimaryKey:@"member_0"]; | |
member.info = [NSUUID UUID].UUIDString; | |
[_realm addOrUpdateObjects:@[member]]; | |
[_realm commitWriteTransaction]; | |
} | |
- (IBAction)changeUser:(id)sender { | |
[_realm beginWriteTransaction]; | |
User *user = [User objectInRealm:_realm forPrimaryKey:@"user_0"]; | |
user.name = [NSUUID UUID].UUIDString; | |
[_realm addOrUpdateObjects:@[user]]; | |
[_realm commitWriteTransaction]; | |
} | |
#pragma mark - Configure | |
- (void)_configureResults { | |
Chat *chat = [Chat objectInRealm:_realm forPrimaryKey:@"first"]; | |
RLMNotificationToken *tok = [chat addNotificationBlock:^(BOOL deleted, NSArray<RLMPropertyChange *> * _Nullable changes, NSError * _Nullable error) { | |
NSLog(@"== receive notification chat =="); | |
}]; | |
_tokenChat = tok; | |
_chat = chat; | |
} | |
- (void)_configureListResults { | |
RLMResults *res = [Chat allObjects]; | |
RLMNotificationToken *tok = [res addNotificationBlock:^(RLMResults * _Nullable results, RLMCollectionChange * _Nullable change, NSError * _Nullable error) { | |
NSLog(@"== receive notification list =="); | |
}]; | |
_tokenList = tok; | |
_list = res; | |
} | |
#pragma mark - Generate | |
- (void)_generateChat { | |
Chat *chat = [Chat new]; | |
chat.Id = @"first"; | |
for (NSInteger i = 0; i < 10; i ++) { | |
User *user = [[User alloc] init]; | |
user.Id = [NSString stringWithFormat:@"user_%lu",i]; | |
user.status = @"hello"; | |
Member *member = [[Member alloc] init]; | |
member.Id = [NSString stringWithFormat:@"member_%lu",i]; | |
member.user = user; | |
[chat.members addObject:member]; | |
} | |
[self _saveChat:chat]; | |
} | |
#pragma mark - Storage | |
- (void)_saveChat:(Chat *)chat { | |
RLMRealm *realm = _realm; | |
[realm beginWriteTransaction]; | |
[realm addOrUpdateObject:chat]; | |
[realm commitWriteTransaction]; | |
} | |
@end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment