Skip to content

Commit 6181a86

Browse files
committed
add transaction example
1 parent 7a220d0 commit 6181a86

File tree

3 files changed

+31
-7
lines changed

3 files changed

+31
-7
lines changed

LKDBHelper/Helper/LKDBHelper.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@
2020

2121
#ifdef DEBUG
2222
#ifdef NSLog
23-
#define LKLog(fmt, ...) NSLog(@"#LKDBHelper ERROR:\n" fmt,##__VA_ARGS__);
23+
#define LKLog(fmt, ...) NSLog(@"#LKDBHelper Message:\n" fmt,##__VA_ARGS__);
2424
#else
25-
#define LKLog(fmt, ...) NSLog(@"\n#LKDBHelper ERROR: %s [Line %d] \n" fmt, __PRETTY_FUNCTION__, __LINE__, ##__VA_ARGS__);
25+
#define LKLog(fmt, ...) NSLog(@"\n#LKDBHelper Message: %s [Line %d] \n" fmt, __PRETTY_FUNCTION__, __LINE__, ##__VA_ARGS__);
2626
#endif
2727
#else
2828
# define LKLog(...)

LKDBHelper/LKAppDelegate.m

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -85,18 +85,39 @@ -(void)test
8585
test.date = [NSDate date];
8686
test.color = [UIColor orangeColor];
8787
test.error = @"nil";
88-
88+
8989
//异步 插入第一条 数据 Insert the first
9090
[globalHelper insertToDB:test];
9191

92+
[globalHelper executeDB:^(FMDatabase *db) {
93+
94+
[db beginTransaction];
95+
96+
test.name = @"1";
97+
[globalHelper insertToDB:test];
98+
test.name = @"2";
99+
[globalHelper insertToDB:test];
100+
101+
test.name = @"3";
102+
test.rowid = 0; //no new object,should set rowid:0
103+
BOOL insertSucceed = [globalHelper insertWhenNotExists:test];
104+
105+
//insert fail
106+
if(insertSucceed == NO)
107+
[db rollback];
108+
else
109+
[db commit];
110+
111+
}];
112+
113+
92114
addText(@"同步插入 完成! Insert completed synchronization");
93115

94116
sleep(1);
95117

96118

97119
//改个 主键 插入第2条数据 update primary colume value Insert the second
98120
test.name = @"li si";
99-
100121
[globalHelper insertToDB:test callback:^(BOOL isInsert) {
101122
addText(@"asynchronization insert complete: %@",isInsert>0?@"YES":@"NO");
102123
}];
@@ -106,8 +127,8 @@ -(void)test
106127
//查询 search
107128
addText(@"同步搜索 sync search");
108129

109-
NSMutableArray* array = [LKTest searchWithWhere:nil orderBy:nil offset:0 count:100];
110-
for (NSObject* obj in array) {
130+
NSMutableArray* arraySync = [LKTest searchWithWhere:nil orderBy:nil offset:0 count:100];
131+
for (NSObject* obj in arraySync) {
111132
addText(@"%@",[obj printAllPropertys]);
112133
}
113134

LKDBHelper/LKTestModels.m

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ -(id)userGetValueForModel:(LKDBProperty *)property
4343
{
4444
if([property.sqlColumeName isEqualToString:@"address"])
4545
{
46+
if(self.address == nil)
47+
return @"";
4648
[LKTestForeign insertToDB:self.address];
4749
return @(self.address.addid);
4850
}
@@ -71,7 +73,8 @@ +(void)columeAttributeWithProperty:(LKDBProperty *)property
7173
}
7274
else if([property.propertyName isEqualToString:@"date"])
7375
{
74-
property.isUnique = YES;
76+
// if you use unique,this property will also become the primary key
77+
// property.isUnique = YES;
7578
property.checkValue = @"MyDate > '2000-01-01 00:00:00'";
7679
property.length = 30;
7780
}

0 commit comments

Comments
 (0)