I have an existing C API written in C that makes heavy use of status codes returned from functions for error handling. I'm trying to understand the preferred method of handling this type of situation with libdispatch.
Here's an example function. The RETURN_ERROR and related macros just call a function that returns an error code and prints a message.
int
mport_db_prepare(sqlite3 *db, sqlite3_stmt **stmt, const char * fmt, ...)
{
va_list args;
char *sql;
va_start(args, fmt);
sql = sqlite3_vmprintf(fmt, args);
va_end(args);
if (sql == NULL)
RETURN_ERROR(MPORT_ERR_FATAL, "Couldn't allocate memory for sql statement");
if (sqlite3_prepare_v2(db, sql, -1, stmt, NULL) != SQLITE_OK) {
SET_ERRORX(MPORT_ERR_FATAL, "sql error preparing '%s': %s", sql, sqlite3_errmsg(db));
sqlite3_free(sql);
RETURN_CURRENT_ERROR;
}
sqlite3_free(sql);
return MPORT_OK;
}
I'd like to use serial queues around the sql access bits and am aware I can use dispatch_sync
to return values. However, I've also read that it's quite easy to get into deadlocks with many sync calls.
What would the best practice be for such an interface? Pass a block with a handler for success/fail? Would I provide a dispatch_queue_t
parameter to let the completion block run on a user specified queue?