Project

General

Profile

Actions

SYNC method - Objective-C

-(void)getRemoteChangesForTable:(NSString*)subscriberId tableName:(NSString*)tableName error:(NSError **)error{
    NSString *requestUrlString = [NSString stringWithFormat:@"%@/Sync/%@/%@", _serverURL, subscriberId, tableName];
    NSURL *requestURL = [NSURL URLWithString:requestUrlString];
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];

    [request setURL:requestURL];
    [request setHTTPMethod:@"GET"];

    NSHTTPURLResponse *response;

    NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:error];

    if(!*error){
        switch (response.statusCode) {
            case 200:
                break;
            default:
                *error = [NSError errorWithDomain:@"com.sqlite-sync" code:0 userInfo:[NSDictionary dictionaryWithObject:[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding] forKey:NSLocalizedDescriptionKey]];
                break;
        }
    }

    if(*error) return;

    NSArray<SQLiteSyncData*> *syncDatas = [SQLiteSyncData arrayOfModelsFromData:data error:error];

    if(*error) return;

    for(SQLiteSyncData *syncData in syncDatas){        
        if([syncData.SyncId intValue] > 0){
            sqlite3 *db;
            sqlite3_stmt *stmt = nil;

            if(sqlite3_open([_databasePath UTF8String], &db) == SQLITE_OK){
                sqlite3_exec(db, "BEGIN TRANSACTION", 0, 0, 0);

                if(!*error && [syncData.TriggerInsertDrop length] > 0){
                    if(sqlite3_prepare_v2(db, [syncData.TriggerInsertDrop UTF8String], -1, &stmt, NULL) != SQLITE_OK
                       || sqlite3_step(stmt) != SQLITE_DONE){
                        *error = [NSError errorWithDomain:@"com.sqlite-sync" code:0 userInfo:[NSDictionary dictionaryWithObject:[NSString stringWithFormat:@"%s", sqlite3_errmsg(db)] forKey:NSLocalizedDescriptionKey]];
                    }
                }
                if(!*error && [syncData.TriggerUpdateDrop length] > 0){
                    if(sqlite3_prepare_v2(db, [syncData.TriggerUpdateDrop UTF8String], -1, &stmt, NULL) != SQLITE_OK
                       || sqlite3_step(stmt) != SQLITE_DONE){
                        *error = [NSError errorWithDomain:@"com.sqlite-sync" code:0 userInfo:[NSDictionary dictionaryWithObject:[NSString stringWithFormat:@"%s", sqlite3_errmsg(db)] forKey:NSLocalizedDescriptionKey]];
                    }
                }
                if(!*error && [syncData.TriggerDeleteDrop length] > 0){
                    if(sqlite3_prepare_v2(db, [syncData.TriggerDeleteDrop UTF8String], -1, &stmt, NULL) != SQLITE_OK
                       || sqlite3_step(stmt) != SQLITE_DONE){
                        *error = [NSError errorWithDomain:@"com.sqlite-sync" code:0 userInfo:[NSDictionary dictionaryWithObject:[NSString stringWithFormat:@"%s", sqlite3_errmsg(db)] forKey:NSLocalizedDescriptionKey]];
                    }
                    sqlite3_reset(stmt);
                }

                NSArray<SQLiteSyncDataRecord*> *records;

                if(!*error){
                    records = [syncData SQLiteSyncDataRecordsWithError:error];
                }

                if(!*error){
                    for(SQLiteSyncDataRecord *record in records){
                        if([record.action intValue] == 1 || [record.action intValue] == 2 || [record.action intValue] == 3){
                            NSString *query;

                            switch ([record.action intValue]) {
                                case 1:
                                    query = syncData.QueryInsert;
                                    break;
                                case 2:
                                    query = syncData.QueryUpdate;
                                    break;
                                case 3:
                                    query = [syncData.QueryDelete stringByAppendingString:@"?"];
                                    break;
                            }

                            if(sqlite3_prepare_v2(db, [query UTF8String], -1, &stmt, NULL) == SQLITE_OK){
                                for(int i = 0; i < [record.columns count]; i++){
                                    sqlite3_bind_text(stmt, i + 1, [[record.columns objectAtIndex:i] UTF8String], -1, SQLITE_TRANSIENT);
                                }

                                if(sqlite3_step(stmt) != SQLITE_DONE){
                                    *error = [NSError errorWithDomain:@"com.sqlite-sync" code:0 userInfo:[NSDictionary dictionaryWithObject:[NSString stringWithFormat:@"%s", sqlite3_errmsg(db)] forKey:NSLocalizedDescriptionKey]];
                                }

                                sqlite3_reset(stmt);
                            }
                            else{
                                *error = [NSError errorWithDomain:@"com.sqlite-sync" code:0 userInfo:[NSDictionary dictionaryWithObject:[NSString stringWithFormat:@"%s", sqlite3_errmsg(db)] forKey:NSLocalizedDescriptionKey]];
                            }

                            if(*error) break;
                        }
                    }
                }

                if(!*error && [syncData.TriggerInsert length] > 0){
                    if(sqlite3_prepare_v2(db, [syncData.TriggerInsert UTF8String], -1, &stmt, NULL) != SQLITE_OK
                       || sqlite3_step(stmt) != SQLITE_DONE){
                        *error = [NSError errorWithDomain:@"com.sqlite-sync" code:0 userInfo:[NSDictionary dictionaryWithObject:[NSString stringWithFormat:@"%s", sqlite3_errmsg(db)] forKey:NSLocalizedDescriptionKey]];
                    }
                    sqlite3_reset(stmt);
                }
                if(!*error && [syncData.TriggerUpdate length] > 0){
                    if(sqlite3_prepare_v2(db, [syncData.TriggerUpdate UTF8String], -1, &stmt, NULL) != SQLITE_OK
                       || sqlite3_step(stmt) != SQLITE_DONE){
                        *error = [NSError errorWithDomain:@"com.sqlite-sync" code:0 userInfo:[NSDictionary dictionaryWithObject:[NSString stringWithFormat:@"%s", sqlite3_errmsg(db)] forKey:NSLocalizedDescriptionKey]];
                    }
                    sqlite3_reset(stmt);
                }
                if(!*error && [syncData.TriggerDelete length] > 0){
                    if(sqlite3_prepare_v2(db, [syncData.TriggerDelete UTF8String], -1, &stmt, NULL) != SQLITE_OK
                       || sqlite3_step(stmt) != SQLITE_DONE){
                        *error = [NSError errorWithDomain:@"com.sqlite-sync" code:0 userInfo:[NSDictionary dictionaryWithObject:[NSString stringWithFormat:@"%s", sqlite3_errmsg(db)] forKey:NSLocalizedDescriptionKey]];
                    }
                    sqlite3_reset(stmt);
                }

                if(*error){
                    sqlite3_exec(db, "ROLLBACK TRANSACTION", 0, 0, 0);
                }
                else{
                    sqlite3_exec(db, "COMMIT", 0, 0, 0);
                }

                if(stmt){
                    sqlite3_finalize(stmt);
                }

                sqlite3_close(db);
            }
            else{
                *error = [NSError errorWithDomain:@"com.sqlite-sync" code:0 userInfo:[NSDictionary dictionaryWithObject:@"Failed to open database" forKey:NSLocalizedDescriptionKey]];
            }

            if(*error) return;

            [self commitSynchronization:syncData.SyncId error:error];

            if(*error) return;
        }
    }
}

Updated by Tomek Dziemidowicz over 5 years ago · 2 revisions

Go to top