Skip to content

Commit

Permalink
feat(ios): have Ti.Database.DB.executeAllAsync return a Promise
Browse files Browse the repository at this point in the history
  • Loading branch information
sgtcoolguy committed Jan 26, 2021
1 parent 7b70f11 commit 6c4253f
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 6 deletions.
2 changes: 1 addition & 1 deletion iphone/Classes/TiDatabaseProxy.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ READONLY_PROPERTY(NSUInteger, rowsAffected, RowsAffected);
- (JSValue *)executeAsync:(NSString *)sql;
- (NSArray<TiDatabaseResultSetProxy *> *)executeAll:(NSArray<NSString *> *)queries;
JSExportAs(executeAllAsync,
-(void)executeAllAsync
-(JSValue *)executeAllAsync
: (NSArray<NSString *> *)queries withCallback
: (JSValue *)callback);
- (void)remove;
Expand Down
18 changes: 13 additions & 5 deletions iphone/Classes/TiDatabaseProxy.m
Original file line number Diff line number Diff line change
Expand Up @@ -379,21 +379,25 @@ - (JSValue *)executeAsync:(NSString *)sql
return results;
}

- (void)executeAllAsync:(NSArray<NSString *> *)queries withCallback:(JSValue *)callback
- (JSValue *)executeAllAsync:(NSArray<NSString *> *)queries withCallback:(JSValue *)callback
{
JSContext *context = [self currentContext];
KrollPromise *promise = [[[KrollPromise alloc] initInContext:context] autorelease];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
JSContext *context = [callback context];
NSError *error = nil;
NSMutableArray *results = [NSMutableArray arrayWithCapacity:[queries count]];
NSUInteger index = 0;
for (NSString *sql in queries) {
TiDatabaseResultSetProxy *result = [self executeSQL:sql withParams:nil withError:&error];
if (error != nil) {
JSValue *jsError = [self createError:@"failed to execute SQL statements" subreason:[error description] location:CODELOCATION inContext:context];
jsError[@"results"] = result;
jsError[@"results"] = results;
jsError[@"index"] = [NSNumber numberWithUnsignedInteger:index];
dispatch_async(dispatch_get_main_queue(), ^{
[callback callWithArguments:@[ jsError, results ]];
if (![callback isUndefined]) {
[callback callWithArguments:@[ jsError, results ]];
}
[promise reject:@[ jsError ]];
});
return;
}
Expand All @@ -406,9 +410,13 @@ - (void)executeAllAsync:(NSArray<NSString *> *)queries withCallback:(JSValue *)c
}

dispatch_async(dispatch_get_main_queue(), ^{
[callback callWithArguments:@[ [JSValue valueWithUndefinedInContext:context], results ]];
if (![callback isUndefined]) {
[callback callWithArguments:@[ [JSValue valueWithUndefinedInContext:context], results ]];
}
[promise resolve:@[ results ]];
});
});
return promise.JSValue;
}

- (void)close
Expand Down

0 comments on commit 6c4253f

Please sign in to comment.