Actions
InitializeSubscriber Objective-C¶
-(void)initializeSubscriber:(nonnull NSString*)subscriberId error:(NSError * _Nullable * _Nullable)error{
NSString *requestUrlString = [NSString stringWithFormat:@"%@/InitializeSubscriber/%@", _serverURL, subscriberId];
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;
NSDictionary* schema = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:error];
if(*error) return;
NSArray *keys = [[schema allKeys] sortedArrayUsingComparator:^NSComparisonResult(id a, id b) { return [a compare:b]; }];
sqlite3 *db;
sqlite3_stmt *stmt = NULL;
if(sqlite3_open([_databasePath UTF8String], &db) == SQLITE_OK){
sqlite3_exec(db, "BEGIN TRANSACTION", 0, 0, 0);
for (NSString* key in keys) {
if(![key containsString:@"00000"]){
if(sqlite3_prepare_v2(db, [[schema objectForKey:key] 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]];
break;
}
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]];
}
}
Updated by Tomek Dziemidowicz over 5 years ago · 1 revisions
Go to top