I'm writing a PostgreSQL library in Objective-C, based on libpq (written in C). While I've built a synchronous querying method that uses PQexec
sucessfully; my asynchronous method never receives the query's results. PQgetResult
always returns NULL
.
I've tried various combinations of connection- and query-checking, but none of them corrected the issue. The query itself is a SELECT
on a table with one row. I can swap out this asynchronous method for the synchronous method and have the desired behavior. I have also checked the conn
pointer I'm retaining is referencing the same object throughout.
I am not using single row mode.
+ (instancetype) resultSetWithQuery: (NSString*) query usingConnection: (PGConnection*) conn whenComplete: (Callback) callback {
PGResultSet* resultSet = [[PGResultSet alloc] init];
NSString* encoding = [conn encoding];
int success = PQsendQuery([conn conn], [query UTF8String]);
[resultSet setEncoding:encoding];
[resultSet setQuery:query];
[resultSet setConn:conn];
if(success){
NSTimeInterval timeInterval = 0.5;
NSTimer* checkStatus = [NSTimer scheduledTimerWithTimeInterval:timeInterval
target:resultSet
selector:@selector(checkIfComplete)
userInfo:nil
repeats:YES];
[resultSet setTimer:checkStatus];
[resultSet setCallback:callback];
}
return resultSet;
}
- (void)checkIfComplete{
if(!PQconsumeInput([[self conn] conn])){
[[self timer] invalidate];
}
if(PQisBusy([[self conn] conn])){
return;
}
// result is always NULL
PGresult* result = PQgetResult([[self conn] conn]);
// ...
}