sqlite3_prepare_v2崩溃 - 已经运行过上一个查询

时间:2015-07-16 08:53:30

标签: ios sqlite grand-central-dispatch

我写了一个在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], -1, 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, sql, -1, &statement, 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, sql, -1, &statement, 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];   
        }
    }];
}

您的想法和解决方案?

3 个答案:

答案 0 :(得分:6)

使用以下命令创建一个串行队列:

dispatch_queue_t myQueue = dispatch_queue_create("My Queue", DISPATCH_QUEUE_SERIAL);

这应该管理对数据库的顺序访问。

答案 1 :(得分:4)

我注意到的一件事是在git add assets/\* 中调用方法assets/file1.txt,但您粘贴了viewWillAppear的正文。这是一个长镜头,但你确定你正确地调用你的getActivities方法?从哪里调用GetAllActivities方法?

答案 2 :(得分:0)

请确保传输良好的sqlite3 *(并检出)。这就是我解决问题的方式