NyaruDBにゃるものを使ってみた話 (+Realm比較)
-
Upload
masaki-oshikawa -
Category
Technology
-
view
258 -
download
0
Transcript of NyaruDBにゃるものを使ってみた話 (+Realm比較)
NyaruDB
@starfruits_j (Little Gleam)
Azione
Azione Web
Swift?!
Nator
© 2014 Azione Co.,Ltd. All Right Reserved.
DB ( )SQL
DB
DB
typo
[NSString stringWithFormat:]
INDEX
SQLite(FMDB)
3GS
FMDatabaseQueue
REINDEX
ALTER TABLE mails ADD COLUMN answered INTEGER DEFAULT ....
...
SparrowOpenSource
OSS !=
TokyoCabinet
NoSQL - SQL
..
API ( )
NoJSON, NoARC NoIB
main.m
int main(int argc, char *argv[])
{
@autoreleasepool {
if ([AppSettings isOS8OrGreater]) {
return UIApplicationMain(argc, argv, nil, NSStringFromClass([MB2AppDelegate class]));
}
return UIApplicationMain(argc, argv, nil, NSStringFromClass([ScreenFactory class]));
}
}
DB SQL
github
NyaruDBhttps://github.com/kelp404/NyaruDB
( )SAN ( )
W
2013 4 7 1:05
MIT License
NyaruDB is a simple NoSQL database in
Objective-C. It could be run on iOS and OS X.
It is a key-valu pair NoSQL database. You could
search data by fields of the document.
DB
SQL
NyaruDB SQL
CocoaPods
SQL
_documentFilePath
_indexFilePath
NSFileHandle seek
Table keyselect
(asyncFetch)AsyncDisplayKit
10
SQLKeyValueStore
NSDictionary NSDictionary
- (NSString *)name { return _dic[@"name"];}
- (void)setName:(NSString *)name { _dic[@"name"] = name;}
JSON OK
NyaruCollection *col = [[NyaruDB instance] collection:@"articles"];[col createIndex:@"user_id"];[col put:@{ @"user_id" : @1 @"name" : @"Cocoa", @"age" : @"test",}];
instance singletoncollection close instance
JSON
- (instansetype)initWithDictionary:(NSDictionary *)dictionary { if ((self = [super init])) { _dic = dictionary; } return self;}
- (void)save { [self.col put:_dic];}
"key" primarykeykey update
SELECTNSArray *results = [[col all] fetch];
WHERENSDictionary *result = [[col where:@"user_id" equal:identifier] fetchFirst];
ANDwhere: => NyaruQuery
[[self.col where:@"user_id" less:100] and:@"user_id" greater:10]];
NyaruQuery- (NyaruQuery *)and:(NSString *)indexName equal:(id)value;- (NyaruQuery *)and:(NSString *)indexName notEqual:(id)value;- (NyaruQuery *)and:(NSString *)indexName less:(id)value;- (NyaruQuery *)and:(NSString *)indexName lessEqual:(id)value;- (NyaruQuery *)and:(NSString *)indexName greater:(id)value;- (NyaruQuery *)and:(NSString *)indexName greaterEqual:(id)value;- (NyaruQuery *)and:(NSString *)indexName like:(NSString *)value;
- (NyaruQuery *)or:(NSString *)indexName equal:(id)value;- (NyaruQuery *)orderBy:(NSString *)indexName;- (NyaruQuery *)orderByDESC:(NSString *)indexName;
: JSON
NSNull nil
limit length of field name is 255limit of documents is 4,294,967,295
limit of document file size is 4Gkey is unique and it is NSStringkey only provides equal search
key is case sensitiveindex is case insensitive
a field of the document should be same data typewhich is index
sort query allow only one
http://realm.io/
Realm is a mobile database: a replacement forSQLite & Core Data Realm can save you
thousands of lines of code & weeks of work, andlets you craft amazing new user experiences.
CocoaPods
SQL
RLMObject
Table Class
@interface User : RLMObject
@property (nonatomic, copy) NSString *identifier;
@property (nonatomic, copy) NSString *name;
@end
SQL
@interface User : RLMObject
@property (nonatomic, copy) NSString *identifier;
@property (nonatomic, copy) NSString *name;
@end
+ (NSString *)primaryKey {
return @"identifier";
}
INSERT[[RLMRealm defaultRealm] transactionWithBlock:^{
[[RLMRealm defaultRealm] addObject:user];
[[RLMRealm defaultRealm] addOrUpdateObject:user];
}];
SELECTRLMResults *users = [User allObjects];
RLMResultsNSArray (for in )
@interface RLMResults : NSObject<RLMCollection, NSFastEnumeration>
...
- (id)objectAtIndex:(NSUInteger)index;
- (id)firstObject;
- (id)lastObject;
...
@end
WHEREUser *user = [[User objectsWhere:@"identifier = %@", identifier] firstObject];
NSPredicate NSPredicate
ANDobjectsWhere: => RLMResults
[[User objectsWhere:@"age < %@", @(20)] objectsWhere:@"age > %@", @(2)]];
RLMResults@interface Picture : RLMObject
@property (nonatomic, assign) BOOL isDeleted;
...
+ (RLMResults *)pictures {
return [self objectsWhere:@"isDeleted = 0"];
}
+ (RLMResults *)deletedPictures {
return [self objectsWhere:@"isDeleted = 1"];
}
+ (RLMResults *)myPictures {
RLMResults *res = [self pictures];
res = [res objectsWhere:@"user_id", [Account identifier]];
res = [res sortedResultsUsingProperty:@"created" ascending:NO];
return res;
}
delete[[RLMRealm defaultRealm] transactionWithBlock:^{
[[RLMRealm defaultRealm] deleteObjects:[Picture myPictures]];
}];
...
Migration [RLMRealm setSchemaVersion:9
withMigrationBlock:^(RLMMigration *migration, NSUInteger oldSchemaVersion) {
if (oldSchemaVersion < 8) {
[migration enumerateObjects:NSStringFromClass([Pictures class])
block:^(RLMObject *oldObject, RLMObject *newObject) {
newObject[@"updated"] = oldObject[@"created"];
}];
}
}];
schemaVersion
[RLMRealm defaultRealm]DBDB
notification_token = [[RLMRealm defaultRealm] addNotificationBlock:^(NSString *notification, RLMRealm *realm) { // code}];
NSMapTable token
AppDelegate migrationViewController initWithCoder:NSURLJava
getter
https://github.com/jstarfruits/BenchDB
normal single main (10,000)FMDB 00.24 49.194 00.617NyaruDB 29.02 17.890 59.186Realm 04.18 28.744 00.385
SQLite
Realm NyaruDB SQL
NyaruDB - Realm - FMDB -
Realm
Azione
azione.co.jp