ios – sqlite3_prepare_v2 crash – 已经运行上一个查询

前端之家收集整理的这篇文章主要介绍了ios – sqlite3_prepare_v2 crash – 已经运行上一个查询前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我写了一个在viewWillAppear上触发的查询.回到该屏幕快速导航导致在sqlite3_prepare_v2崩溃.
我假设的是,崩溃是由于以前的查询未完成.
我正在使用dispatch_async调用数据库操作,因为我首先更新远程数据,然后将其保存在数据库中.
代码如下所示:
dispatch_queue_t myQueue = dispatch_queue_create("My Queue",NULL);

                 dispatch_async(myQueue,^{
                    [self parseActivityResponse:data];  // sqlite operation here
                     dispatch_async(dispatch_get_main_queue(),^{
                         // Update the UI
                         if(loaderImageview)
                            [loaderImageview removeFromSuperview];
                         [tableActivities reloadData];

                     });
                 });

查询如下:

- (NSMutableArray*)GetAllInvitations:(NSString*)ActivityId
{
    NSMutableArray *arrInvitations = [[NSMutableArray alloc]init];

    sqlite3_stmt *statement = nil;

    @try {
        const char *sql = "SELECT * FROM Invitation where ActivityId = ?";

        if (sqlite3_prepare_v2(database,sql,-1,&statement,NULL) == sqlITE_OK) {

            sqlite3_bind_text(statement,1,[ActivityId UTF8String],sqlITE_TRANSIENT);
            while (sqlite3_step(statement) == sqlITE_ROW) {

                char *dbString;

                Invitation *invitation = [[Invitation alloc]init];

                dbString = (char *)sqlite3_column_text(statement,0);
                NSString *Id = (dbString) ? [NSString stringWithUTF8String:dbString] : @"";
                [invitation setId:Id];

                dbString = (char *)sqlite3_column_text(statement,1);
                NSString *childName = (dbString) ? [NSString stringWithUTF8String:dbString] : @"";
                [invitation setChildName:childName];

                dbString = (char *)sqlite3_column_text(statement,2);
                NSString *response = (dbString) ? [NSString stringWithUTF8String:dbString] : @"";
                [invitation setResponse:response];

                dbString = (char *)sqlite3_column_text(statement,3);
                NSString *invitationId = (dbString) ? [NSString stringWithUTF8String:dbString] : @"";
                [invitation setInvitationId:invitationId];

                dbString = (char *)sqlite3_column_text(statement,4);
                NSString *isCoGaurdian = (dbString) ? [NSString stringWithUTF8String:dbString] : @"";
                [invitation setIsCoGaurdian:isCoGaurdian];

                dbString = (char *)sqlite3_column_text(statement,5);
                NSString *activityId = (dbString) ? [NSString stringWithUTF8String:dbString] : @"";
                [invitation setActivityId:activityId];

                dbString = (char *)sqlite3_column_text(statement,6);
                NSString *picture = (dbString) ? [NSString stringWithUTF8String:dbString] : @"";
                [invitation setPicture:picture];

                dbString = (char *)sqlite3_column_text(statement,7);
                NSString *invitationUserId = (dbString) ? [NSString stringWithUTF8String:dbString] : @"";
                [invitation setInvitedUserId:invitationUserId];

                dbString = (char *)sqlite3_column_text(statement,8);
                NSString *roleId = (dbString) ? [NSString stringWithUTF8String:dbString] : @"";
                [invitation setRoleId:roleId];

                dbString = (char *)sqlite3_column_text(statement,9);
                NSString *status = (dbString) ? [NSString stringWithUTF8String:dbString] : @"";
                [invitation setStatus:status];

                [arrInvitations addObject:invitation];
            }
        }
        else
        {
            NSLog(@"Failed to create table: %s",sqlite3_errmsg(database));
        }


    }
    @catch (NSException *exception) {

        NSLog(@"Error in GetAllInvitations : %@",exception.description);
    }
    @finally {

        sqlite3_finalize(statement);
    }

    return arrInvitations;
}  


- (NSMutableArray*)GetAllActivities{

    NSMutableArray *arrProjects = [[NSMutableArray alloc]init];

    sqlite3_stmt *statement = nil;

    @try {
        const char *sql = "SELECT * FROM Activity";

        if (sqlite3_prepare_v2(database,NULL) == sqlITE_OK) {
            NSLog(@"success");
            while (sqlite3_step(statement) == sqlITE_ROW) {

                .........

                activity.arrInvitations = [self GetAllInvitations:activityId];

                [arrProjects addObject:activity];
            }
        }
        else
        {
            NSLog(@"Prepare-error #%i: %s",sqlite3_prepare_v2(database,NULL),sqlite3_errmsg(database));
        }
    }
    @catch (NSException *exception) {

        NSLog(@"Error in GetAllActivities : %@",exception.description);
    }
    @finally {

        sqlite3_finalize(statement);
    }

    return arrProjects;
}

我从viewWillAppear调用GetAllActivities:

-(void) viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self reloadData];
}

-(void)reloadData
{
    [self getRemoteActivities:^(BOOL finished) {
        if(finished){
            if([TGProjectHandler hasExistingSession])
            {
                [self getActivities];

            }
            else
                [self login];   
        }
    }];
}

你的想法和解决方案?

解决方法

创建一个串行队列:
dispatch_queue_t myQueue = dispatch_queue_create("My Queue",DISPATCH_QUEUE_SERIAL);

并且应该管理对数据库的顺序访问.

原文链接:https://www.f2er.com/iOS/336017.html

猜你在找的iOS相关文章