Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support Transaction #31

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions YTKKeyValueStore/YTKKeyValueStore.h
Expand Up @@ -37,6 +37,8 @@

- (void)putObject:(id)object withId:(NSString *)objectId intoTable:(NSString *)tableName;

- (void)putObjects:(NSArray *)keyValueItems intoTable:(NSString *)tableName;

- (id)getObjectById:(NSString *)objectId fromTable:(NSString *)tableName;

- (YTKKeyValueItem *)getYTKKeyValueItemById:(NSString *)objectId fromTable:(NSString *)tableName;
Expand Down
32 changes: 32 additions & 0 deletions YTKKeyValueStore/YTKKeyValueStore.m
Expand Up @@ -182,6 +182,38 @@ - (void)putObject:(id)object withId:(NSString *)objectId intoTable:(NSString *)t
}
}

- (void)putObjects:(NSArray *)keyValueItems intoTable:(NSString *)tableName
{
if ([YTKKeyValueStore checkTableName:tableName] == NO) {
return;
}
__block BOOL result;
[_dbQueue inTransaction:^(FMDatabase *db, BOOL *rollback) {
for (YTKKeyValueItem *item in keyValueItems) {
id object = item.itemObject;
NSString *objectId = item.itemId;

NSError * error;
NSData * data = [NSJSONSerialization dataWithJSONObject:object options:0 error:&error];
if (error) {
debugLog(@"ERROR, faild to get json data");
continue;
}

NSString * jsonString = [[NSString alloc] initWithData:data encoding:(NSUTF8StringEncoding)];
NSDate * createdTime = [NSDate date];
NSString * sql = [NSString stringWithFormat:UPDATE_ITEM_SQL, tableName];

result = [db executeUpdate:sql, objectId, jsonString, createdTime];
if (!result) {
*rollback = YES;
debugLog(@"ERROR, rollback to insert/replace into table: %@", tableName);
return;
}
}
}];
}

- (id)getObjectById:(NSString *)objectId fromTable:(NSString *)tableName {
YTKKeyValueItem * item = [self getYTKKeyValueItemById:objectId fromTable:tableName];
if (item) {
Expand Down