samedi 9 janvier 2016

IOS SQLite select from return only 1 row

I have a problem after adding new rows to my sqlite database.

In this code I get these results:


  • "idList = 1" (1 row in IOS database).
  • "updatedData = 3" (3 rows in Parse server.

After updateRow method I added the new rows to database and I updated the lastUpdateDate parameter for next loading. The problem is that after I get into getGroupsByUserId the second time (bolded), the idList parameter is again 1 and not 3. I think that the rows were added to the database but I can't read them properly.

-(void)getAllGroupsOfUser:(void(^)(NSMutableArray*))block{
    dispatch_queue_t myQueue =    dispatch_queue_create("myQueueName", NULL);
    dispatch_async(myQueue, ^{
        //long operation
        NSMutableArray* idList = (NSMutableArray*)[sqlUserGroupModelImpl getGroupsByUserId:self.user];
        NSString* lastUpdate = [sqlUserModelImpl getUsersLastUpdateDate];
        NSMutableArray* updatedData;
        if (lastUpdate != nil){
            updatedData = (NSMutableArray*)[parseUserGroupModelImpl getGroupsByUserIdFromDate:lastUpdate userId:self.user];
        }else{
            updatedData = (NSMutableArray*)[parseUserGroupModelImpl getGroupsByUserId:self.user];
        }
        if (updatedData.count > 0) {
            [sqlUserGroupModelImpl updateRow:updatedData];
            [sqlUserGroupModelImpl setUserGroupLastUpdateDate:[NSString stringWithFormat:@"%f",[[NSDate date] timeIntervalSince1970]]];

idList = (NSMutableArray*)[sqlUserGroupModelImpl getGroupsByUserId:self.user];

        }

        //get data of my groups
        NSMutableArray* myGroupsListData = [[NSMutableArray alloc] init];

        for (int i = 0 ; i < [idList count] ; i++) {
            ModelUserGroup* ug = [idList objectAtIndex:i];
            Group* gr = [[ModelGroup instance] getGroup:ug.userId];
            [myGroupsListData addObject:gr];
        }

The other methods:

    +(NSMutableArray*)getUsersByGroupId:(sqlite3*)database groupId:(NSString*)groupId{
        NSMutableArray* data = [[NSMutableArray alloc] init];
        sqlite3_stmt *statment;
        NSString* query = [NSString stringWithFormat:@"SELECT * from USERS_GROUPS where %@ = %@;", GROUP_ID, groupId];

        if (sqlite3_prepare_v2(database,[query UTF8String],-1,&statment,nil) == SQLITE_OK){
            while(sqlite3_step(statment) == SQLITE_ROW){
                NSString* usId = [NSString stringWithFormat:@"%s",sqlite3_column_text(statment,0)];
                NSString* grId = [NSString stringWithFormat:@"%s",sqlite3_column_text(statment,1)];

                ModelUserGroup* ug = [[ModelUserGroup alloc] init:usId groupId:grId];
                [data addObject:ug];
            }
        }else{
            NSLog(@"ERROR: getUsersByGroupId failed %s",sqlite3_errmsg(database));
            sqlite3_finalize(statment);
            return nil;
        }
        sqlite3_finalize(statment);
        return data;

    }

+(void)updateRow:(sqlite3*)database rowToUpdate:(NSArray*)rowToUpdate{
    for (ModelUserGroup* ug in rowToUpdate) {
        [UsersGroupsRelationShipSql addRow:database userId:ug.userId groupId:ug.groupId ];
    }
}

+(void)addRow:(sqlite3*)database userId:(NSString*)userId groupId:(NSString*)groupId{
    sqlite3_stmt *statment;
    NSString* query = [NSString stringWithFormat:@"INSERT OR REPLACE INTO %@ (%@,%@) values (?,?);",USER_GROUPS_TABLE,USER_ID,GROUP_ID];

    if (sqlite3_prepare_v2(database,[query UTF8String],-1,&statment,nil) == SQLITE_OK){
        sqlite3_bind_text(statment, 1, [userId UTF8String],-1,NULL);
        sqlite3_bind_text(statment, 2, [groupId UTF8String],-1,NULL);
        if(sqlite3_step(statment) == SQLITE_DONE){
            sqlite3_finalize(statment);
            return;
        }
    }
    sqlite3_finalize(statment);
    NSLog(@"ERROR: addRowToTable failed %s",sqlite3_errmsg(database));
}

Aucun commentaire:

Enregistrer un commentaire