Mike Keskinov
Mike Keskinov

Reputation: 11878

Response time limit in SQLite

Is there way to limit response time on SQLite?

I have SQL request, which executes from 0 to 10 seconds depends on device performance. This SQL is OPTIONAL. If request takes more than 3 sec, it should be terminated. I would prefer do not run this SQL if I could know apriori that it takes more than 3 sec.

Upvotes: 0

Views: 482

Answers (1)

Anurag
Anurag

Reputation: 141869

Use the sqlite3_progress_handler interface to register a callback function.

As @JeremyP suggested, you can pass in a a pointer to NSDate, or something else storing time as context to compare against. If it reaches above a certain threshold then return a non-zero value from the callback registered with this function to terminate the operation.

int progressHandler(void *context) {
    NSDate *start = (NSDate *)context;
    NSTimeInterval wait = -[start timeIntervalSinceNow];

    if (wait > 3.0) {
        return 1;
    } else {
        return 0;
    }
}

The second argument to this function is how often your callback should be invoked. This value is in # of jump opcodes executed and will vary by machine. For context, on my Macbook, it processes around 20 million opcodes every second. A decent number for your use-case might be 10K-100K.

sqlite3_progress_handler(db, 100000, &progressHandler, (void *)someDate);

Upvotes: 2

Related Questions