iOS中FMDB数据库之增删改查使用实例

2020-01-20 12:47:44王旭

multithread:


- (void)multithread {
  NSLog(@"%s", __func__);

  FMDatabaseQueue *queue = [FMDatabaseQueue databaseQueueWithPath:self.dbPath];
  dispatch_queue_t q1 = dispatch_queue_create("queue1", NULL);
  dispatch_queue_t q2 = dispatch_queue_create("queue2", NULL);

  dispatch_async(q1, ^{
    for (int i = 0; i < 100; ++i) {
      [queue inDatabase:^(FMDatabase *db) {
        NSString *sql = @"insert into user (name, password) values(?, ?) ";
        NSString *name = [NSString stringWithFormat:@"queue111 %d", i];
        BOOL res = [db executeUpdate:sql, name, @"boy"];
        if (!res) {
          NSLog(@"error to add db data: %@", name);
        } else {
          NSLog(@"success to add db data: %@", name);
        }
      }];
    }
  });

  dispatch_async(q2, ^{
    for (int i = 0; i < 100; ++i) {
      [queue inDatabase:^(FMDatabase *db) {
        NSString *sql = @"insert into user (name, password) values(?, ?) ";
        NSString *name = [NSString stringWithFormat:@"queue222 %d", i];
        BOOL res = [db executeUpdate:sql, name, @"boy"];
        if (!res) {
          NSLog(@"error to add db data: %@", name);
        } else {
          NSLog(@"success to add db data: %@", name);
        }
      }];
    }
  });
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持ASPKU。


注:相关教程知识阅读请移步到IOS开发频道。