dispatch_sync中的代码执行失败

时间:2017-12-02 15:13:42

标签: ios objective-c fmdb

我有以下简单的代码无法正确执行。此项应该使用FMDatabaseQueue(_queue)将对象数组写入sqlite数据库,该NSLog()(_queue)在初始化时被实例化为静态值。这是在单身人士中执行的。注释掉dispatch_async包装会导致正确执行,如dispatch_async语句所示。如您所见,我希望在调用时在后台队列中执行此写操作。对dispatch_async的其他调用在应用程序中正常工作(但不在单例内),所有FMDB操作都不在 -(void)addSafePersonEntryArray:(NSMutableArray *)per { NSLog(@"Reaches here with dispatch_async enabled"); dispatch_async( dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0),^{ //commenting out this NSLog(@"But not here"); [_queue inTransaction:^(FMDatabase *db, BOOL *rollBack) { NSLog(@"Nor here"); [db setLogsErrors:YES]; [db setTraceExecution:NO]; [db setKey:self.secretKey]; for(PersonObject *p in per){ [self _addPersonEntryRecord:p db:db]; } }]; }); // and commenting out this as well as dispatch_async causes all NSLog() statements to be reached and the code works properly. } 包装器中。

+ (id)sharedManager {
    static AppManager *sharedAppManager = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        sharedAppManager = [[self alloc] init];
    });
    return sharedAppManager;
}

我像这样实例化单身:

.txt

我将不胜感激任何建议!

0 个答案:

没有答案