您的位置:首页 > 其它

realm simple

2015-07-19 16:01 155 查看
Realm Objective-C

The documentation can be found at realm.io/docs/objc/latest.
The API reference is located at realm.io/docs/objc/latest/api.

https://realm.io/cn/docs/objc/latest/

@interface Person : RLMObject
@property NSString      *name;
@property RLMArray<Dog> *dogs;
@end

@implementation Person
@end

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.rootViewController = [[UIViewController alloc] init];
[self.window makeKeyAndVisible];

[[NSFileManager defaultManager] removeItemAtPath:[RLMRealm defaultRealmPath] error:nil];

// Create a standalone object
Dog *mydog = [[Dog alloc] init];

// Set & read properties
mydog.name = @"Rex";
mydog.age = 9;
NSLog(@"Name of dog: %@", mydog.name);

// Realms are used to group data together
RLMRealm *realm = [RLMRealm defaultRealm]; // Create realm pointing to default file

// Save your object
[realm beginWriteTransaction];
[realm addObject:mydog];
[realm commitWriteTransaction];

// Query
RLMResults *results = [Dog objectsInRealm:realm where:@"name contains 'x'"];

// Queries are chainable!
RLMResults *results2 = [results objectsWhere:@"age > 8"];
NSLog(@"Number of dogs: %li", (unsigned long)results2.count);

// Link objects
Person *person = [[Person alloc] init];
person.name = @"Tim";
[person.dogs addObject:mydog];

[realm beginWriteTransaction];
[realm addObject:person];
[realm commitWriteTransaction];

// Multi-threading
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
RLMRealm *otherRealm = [RLMRealm defaultRealm];
RLMResults *otherResults = [Dog objectsInRealm:otherRealm where:@"name contains 'Rex'"];
NSLog(@"Number of dogs: %li", (unsigned long)otherResults.count);
});

----------
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: