sqlite3_exec在prod配置中崩溃,但它在调试

时间:2016-07-06 21:24:28

标签: ios database sqlite crash-reports

我在iOS应用中使用SQLite。它在prod版本中出现了崩溃,但它在调试配置中运行良好。

所以,我有以下崩溃日志:

Date/Time:           2016-07-07 00:09:53.53 +0300
Launch Time:         2016-07-07 00:09:31.31 +0300
OS Version:          iOS 9.3.2 (13F69)
Report Version:      105

Exception Type:  00000020
Exception Codes: 0x000000008badf00d
Exception Note:  SIMULATED (this is NOT a crash)
Highlighted by Thread:  0

Application Specific Information:
ru.cookntalk.CookntalkApp failed to scene-create after 19.85s (launch took 0.15s of total time limit 20.00s)

Elapsed total CPU time (seconds): 25.700 (user 25.700, system 0.000), 64% CPU 
Elapsed application CPU time (seconds): 16.300, 41% CPU

Filtered syslog:
None found

Thread 0 name:  Dispatch queue: com.apple.main-thread
Thread 0:
0   libsystem_kernel.dylib          0x229e86ac pread + 20
1   libsqlite3.dylib                0x231fb066 0x23190000 + 438374
2   libsqlite3.dylib                0x23197b9c 0x23190000 + 31644
3   libsqlite3.dylib                0x231b0724 0x23190000 + 132900
4   libsqlite3.dylib                0x231afabe 0x23190000 + 129726
5   libsqlite3.dylib                0x231cbb64 0x23190000 + 244580
6   libsqlite3.dylib                0x231cca68 0x23190000 + 248424
7   libsqlite3.dylib                0x231c0c74 0x23190000 + 199796
8   libsqlite3.dylib                0x231bfa50 sqlite3_step + 472
9   libsqlite3.dylib                0x231990ee sqlite3_exec + 402
10  Cookntalk                       0x000f88c0 +[SQLiteAccess executeSQL:withCallback:context:] (SQLiteAccess.m:119)
11  Cookntalk                       0x000f8dda +[SQLiteAccess updateWithSQL:] (SQLiteAccess.m:177)
12  Cookntalk                       0x00100f76 +[HTTPRequest getRecipesDeatil:] (HTTPRequest.m:412)
13  Cookntalk                       0x00101428 +[HTTPRequest updateDB] (HTTPRequest.m:465)
14  Cookntalk                       0x00109ac4 -[AppDelegate application:didFinishLaunchingWithOptions:] (AppDelegate.m:43)
15  UIKit                           0x273c0a6e -[UIApplication _handleDelegateCallbacksWithOptions:isSuspended:restoreState:] + 394
16  UIKit                           0x275ea286 -[UIApplication _callInitializationDelegatesForMainScene:transitionContext:] + 3086
17  UIKit                           0x275ee240 -[UIApplication _runWithMainScene:transitionContext:completion:] + 1588
18  UIKit                           0x27602810 __84-[UIApplication _handleApplicationActivationWithScene:transitionContext:completion:]_block_invoke3286 + 36
19  UIKit                           0x275eb766 -[UIApplication workspaceDidEndTransaction:] + 134
20  FrontBoardServices              0x2443bbf6 __FBSSERIALQUEUE_IS_CALLING_OUT_TO_A_BLOCK__ + 18
21  FrontBoardServices              0x2443baa6 -[FBSSerialQueue _performNext] + 226
22  FrontBoardServices              0x2443bda4 -[FBSSerialQueue _performNextFromRunLoopSource] + 44
23  CoreFoundation                  0x22d2f9e6 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 14
24  CoreFoundation                  0x22d2f5d6 __CFRunLoopDoSources0 + 454
25  CoreFoundation                  0x22d2d93e __CFRunLoopRun + 806
26  CoreFoundation                  0x22c7c1c8 CFRunLoopRunSpecific + 516
27  CoreFoundation                  0x22c7bfbc CFRunLoopRunInMode + 108
28  UIKit                           0x273b9f36 -[UIApplication _run] + 526
29  UIKit                           0x273b4434 UIApplicationMain + 144
30  Cookntalk                       0x00121e74 main (main.m:14)
31  libdyld.dylib                   0x22928872 start + 2

我正在使用以下代码:

+ (NSNumber *)executeSQL:(NSString *)sql withCallback:(void *)callbackFunction context:(id)contextObject {
    NSString *path = [self pathToDB];

   //NSLog(@"Database:   %@",path);

    if (!sql) return nil;

    sqlite3 *db = NULL;
    int rc = SQLITE_OK;
    NSInteger lastRowId = 0;
    int flags = SQLITE_OPEN_FULLMUTEX | SQLITE_OPEN_READWRITE |  SQLITE_OPEN_CREATE;
    rc = sqlite3_open_v2([path UTF8String], &db, flags, NULL);
    if(SQLITE_OK != rc) {
        NSLog(@"Can't open database: %s\n", sqlite3_errmsg(db));
        sqlite3_close(db);
        return nil;
    } else {
        char *zErrMsg = NULL;
        rc = sqlite3_exec(db, [sql UTF8String], callbackFunction, (void*)contextObject, &zErrMsg);



        if(SQLITE_OK != rc) {
            NSLog(@"Can't run query '%@' error message: %s\n", sql, sqlite3_errmsg(db));
            sqlite3_free(zErrMsg);
        }
        lastRowId = sqlite3_last_insert_rowid(db);
        sqlite3_close(db);
    }
    NSNumber *lastInsertRowId = nil;
    if(0 != lastRowId) {
        lastInsertRowId = [NSNumber numberWithInteger:lastRowId];
    }
    return lastInsertRowId;
}

崩溃:

rc = sqlite3_exec(db, [sql UTF8String], callbackFunction, (void*)contextObject, &zErrMsg);

有人有什么想法吗?

2 个答案:

答案 0 :(得分:1)

0x8badf00d(“吃不好的食物”; lol)意味着您的应用程序被监视程序进程终止。请参阅Technical Note 2151,其中包含

  

异常代码0x8badf00d表示iOS已终止应用程序,因为发生了监视程序超时。应用程序花费太长时间来启动,终止或响应系统事件。 ...无论操作是什么,线程0都需要移动到后台线程,或者以不同方式处理,以便它不会阻塞主线程。

看起来您可能正在阻止主线程执行如此多的查询。我建议您确保在后台线程上执行此操作或以其他方式调整此实现。

此外,如果您要进行大量更新,请考虑使用transactions。如果您没有使用事务,但正在进行许多更新,那么如果在事务中包含一系列更新,则可能会获得显着的性能提升。

答案 1 :(得分:0)

我觉得这个问题并不是你的sqlite代码有问题(虽然任何需要运行20秒以上的进程才能进行优化)但是你从didFinishLaunching运行这段代码:withOptions AppDelegate的方法在某种程度上。

如果didFinishLaunching:withOptions未在应用启动后的20秒内完成,iOS将终止您的应用。