+ All Categories
Home > Engineering > Useful and Practical Functionalities in Realm

Useful and Practical Functionalities in Realm

Date post: 14-Jul-2015
Category:
Upload: yusuke-kita
View: 924 times
Download: 7 times
Share this document with a friend
31
Useful and practical functionalities in Realm cocoa Realm meetup #1 trippiece Inc. @kitasuke
Transcript
Page 1: Useful and Practical Functionalities in Realm

Useful and practicalfunctionalitiesin Realm cocoa

Realm meetup #1trippiece Inc.@kitasuke

Page 2: Useful and Practical Functionalities in Realm
Page 3: Useful and Practical Functionalities in Realm
Page 4: Useful and Practical Functionalities in Realm

Table of contents

- Encryption- Migration- Multiprocess- Tips & Tools

Page 5: Useful and Practical Functionalities in Realm

Encryption

Page 6: Useful and Practical Functionalities in Realm

Realm supports encrypting the database file on disk with AES-256+SHA2 by supplying a 64-byte encryption key when creating a Realm.Make sure that the encryption key should be set before opening any Realm

Page 7: Useful and Practical Functionalities in Realm

Open a Realm with an encryption key

// Open the encrypted Realm fileRLMRealm *realm = [RLMRealm realmWithPath:[RLMRealm defaultRealmPath] encryptionKey:encryptionKey readOnly:NO error:nil];

Page 8: Useful and Practical Functionalities in Realm

Realm can store the encryption key in memory

// Set the encryption key for the default Realm[RLMRealm setEncryptionKey:key forRealmsAtPath:[RLMRealm defaultRealmPath]];RLMRealm *realm = [RLMRealm defaultRealm];

Page 9: Useful and Practical Functionalities in Realm

Set the encryption key to unencrypted Realm

@autoreleasepool { RLMRealm *realm = [RLMRealm defaultRealm]; [[NSFileManager defaultManager] removeItemAtPath:[RLMRealm defaultRealmPath] error:nil];

[realm beginWriteTransaction]; [realm writeCopyToPath:[RLMRealm defaultRealmPath] encryptionKey:encryptionKey error:nil]; [realm commitWriteTransaction];}

[[RLMRealm defaultRealm] writeCopyToPath:pathToRealmCopy encryptionKey:encryptionKey error:nil]; needs to be wrapped in @autoreleasepool { }

Page 10: Useful and Practical Functionalities in Realm

Not supported using encrypted Realms with a debugger attached to the process prior to 0.91.0

+ (RLMRealm *)encryptedRealm{#ifdef DEBUG return [RLMRealm defaultRealm];#endif return [RLMRealm realmWithPath:[RLMRealm defaultRealmPath] encryptionKey:encryptionKey readOnly:NO error:nil];}

Page 11: Useful and Practical Functionalities in Realm

Not supported opening encrypted Realm on Realm browser app

Page 12: Useful and Practical Functionalities in Realm

A few critical bugs fixed in 0.91.0

- Crashes on ARM64- Crashes when changing encryption key- Not actually writing the entire file, and a crash after large commits

Page 13: Useful and Practical Functionalities in Realm

Migration

Page 14: Useful and Practical Functionalities in Realm

migration block provides all the logic for converting data models from previous schemas to the new schemaMake sure that migration should be done before opening any Realm

Page 15: Useful and Practical Functionalities in Realm

Add a new property

Previous schema

@interface Company@property NSInteger ID;@end

New schema

@interface Company@property NSInteger ID;@property NSString *name; // new one@end

Page 16: Useful and Practical Functionalities in Realm

Set a new property type with default value

RLMMigrationBlock migrationBlock = ^(RLMMigration *migration, NSUInteger oldSchemaVersion) { if (oldSchemaVersion < 1) { [migrationBlock enumerateObjects:[Company className] block:^(RLMObject *oldObject, RLMObject *newObject) { Company *company = (Company *)newObject; [company setObject:@(RLMPropertyTypeString) forKeyedSubscript:@"name"]; } }}[RLMRealm setSchemaVersion:1 forRealmAtPath:[RLMRealm defaultRealmPath] withMigrationBlock:migrationBlock];

@implementation Company+ (NSDictionary *)defaultPropertyValues{ return @{ @"name": @"company name" };}@end

Page 17: Useful and Practical Functionalities in Realm

Set default value for a new property

RLMMigrationBlock migrationBlock = ^(RLMMigration *migration, NSUInteger oldSchemaVersion) { if (oldSchemaVersion < 1) { [migrationBlock enumerateObjects:[Company className] block:^(RLMObject *oldObject, RLMObject *newObject) { Company *company = (Company *)newObject; company.name = @"company name"; } }}[RLMRealm setSchemaVersion:1 forRealmAtPath:[RLMRealm defaultRealmPath] withMigrationBlock:migrationBlock];

Page 18: Useful and Practical Functionalities in Realm

Add new property having one to one relationships

New schema

@interface Company@property NSInteger ID;@property NSString *name;@property Employee *employee; // new one@end

RLMMigrationBlock migrationBlock = ^(RLMMigration *migration, NSUInteger oldSchemaVersion) { if (oldSchemaVersion < 1) { [migrationBlock enumerateObjects:[Company className] block:^(RLMObject *oldObject, RLMObject *newObject) { Company *company = (Company *)newObject; company.employee = [Employee new]; } }}[RLMRealm setSchemaVersion:1 forRealmAtPath:[RLMRealm defaultRealmPath] withMigrationBlock:migrationBlock];

Page 19: Useful and Practical Functionalities in Realm

Multiprocess

Page 20: Useful and Practical Functionalities in Realm

Seamlessly sharing Realm files between multiple processes for App Extensions

Page 21: Useful and Practical Functionalities in Realm

Put the Realm file in your app group container// Get the shared directory for the application groupNSURL *container = [[NSFileManager defaultManager] containerURLForSecurityApplicationGroupIdentifier:@"group.com.example.my-app"];

// Set the default realm path to a location in the shared container// You can also pass a path in the container to +[RLMRealm realmWithPath:], of courseNSURL *realmURL = [containerURL URLByAppendingPathComponent:@"default.realm"];[RLMRealm setDefaultRealmPath:realmURL.path];

Page 22: Useful and Practical Functionalities in Realm

Current limitations

- Encrypted Realms cannot be shared between processes.- All processes must be running on a single machine, and must be the same architecture (so no sharing Realm files on a network drive, and opening a Realm in both an iOS simulator and an OS X application requires using a 64-bit simulator).- All processes must be using the same version of Realm Cocoa, and must have identical object schemas.

Page 23: Useful and Practical Functionalities in Realm

tips & tools

Page 24: Useful and Practical Functionalities in Realm

use grep with 'RLMException'@throw RLMException(@"Cannot set schema version for Realms that are already open.");@throw RLMException(@"Encryption key must be exactly 64 bytes long");@throw RLMException(@"Custom schema only supported when using dynamic Realms");@throw RLMException(@"The notification block should not be nil");@throw RLMException(@"The Realm is already in a write transaction");@throw RLMException(@"Invalid array type - container must be an RLMArray, RLMArray, or NSArray of RLMObjects");@throw RLMException(@"Cannot migrate Realms that are already open.");@throw RLMException(@"Can't mutate a persisted array outside of a write transaction.");@throw RLMException(@"Object type does not match RLMResults");@throw RLMException(@"RLMArray properties require a protocol defining the contained type - example: RLMArray<Person>");@throw RLMException([NSString stringWithFormat:@"Migration is required for object type '%@' due to the following errors:\n- %@", objectSchema.className, [exceptionMessages componentsJoinedByString:@"\n- "]]);

Page 25: Useful and Practical Functionalities in Realm

ues grep with 'FIXME'// FIXME: make sure delete rules don't purge objects// FIXME - enable when Any supports links// FIXME - enable for ints when we have core suppport// FIXME: Add condition to check for Mixed once it can support a nil value.// FIXME: int64_t should be DateTime but that doesn't work on 32 bit// FIXME: for new apis only return swift initialized values// FIXME - remove deleted tables// FIXME: this should be supported by core in some way

Page 26: Useful and Practical Functionalities in Realm

take a look at CHANGELOG.md+Master Release notes+=============================================================++### Enhancements++* The browser will automatically refresh when the Realm has been modified+ from another process.+ 0.91.0 Release notes (2015-03-10) =============================================================

Page 27: Useful and Practical Functionalities in Realm

take a look at Issues

- LLDB hangs when debugging an application that is using an encrypted Realm.- Support Carthage prebuilt framework- App crashes on read when using writeCopyToPath: to compact encrypted realm- Add RLMConfiguration- Support querying "%@ IN (RLMArray keypath)"- multiple realms contain all classes- Swift: Nested JSON Objects

Page 28: Useful and Practical Functionalities in Realm

take a look at Google Groups

- SQL Join in Realm- HOW TO FETCH ALL CHILD NODE DATA??- Integer to String conversion- Querying by large number of Integer values via predicate with IN clause- Realm Online Office Hours next week - Ask us anything :)- Fine grained notifications and core open sourcing - Time Frame

Page 29: Useful and Practical Functionalities in Realm

Realm + JSONhttps://github.com/matthewcheok/Realm-JSON

+ (NSDictionary *)JSONInboundMappingDictionary { return @{ @"episode.title": @"title", @"episode.description": @"subtitle", @"episode.id": @"episodeID", @"episode.episode_number": @"episodeNumber", @"episode.episode_type": @"episodeType", @"episode.thumbnail_url": @"thumbnailURL", @"episode.published_at": @"publishedDate", };}

Page 30: Useful and Practical Functionalities in Realm

JSONExporthttps://github.com/Ahmed-Ali/JSONExport

Page 31: Useful and Practical Functionalities in Realm

Any questions?


Recommended