我在iOS上使用我的Cordova应用程序从一次重大的位置更改启动,有时当它以这种方式启动时,它会在打开数据库时崩溃。我不确定导致这种情况的原因 - 我自己还没有能够重现它,我只是报道了Crashlytics;它看起来只影响一些设备。
这是我的代码:
class Database {
static let DB_DIR:URL = {
// Library/LocalDatabase doesn't get backed up to iTunes/iCloud, and it matches our sqlite plugin's default (for Javascript access)
let fileMgr = FileManager.default
let libUrl:URL = try! fileMgr.url(for: .libraryDirectory, in: .userDomainMask, appropriateFor: nil, create: false)
return libUrl.appendingPathComponent("LocalDatabase")
}()
static func getPath(_ dbName:String!) -> String {
return DB_DIR.appendingPathComponent(dbName).path
}
static func createDb(_ dbName:String!) -> FMDatabase {
return FMDatabase(path: getPath(dbName))
}
// only for front- and back-end dbs (TODO - close these when app exits, or open/close for each transaction?)
static func getOpenedDb(_ dbName:String) -> FMDatabase {
let db:FMDatabase = createDb(dbName)
if !db.open() {
fatalError("Unable to open \(dbName)")
}
return db
}
static let BACKEND_DB:FMDatabase = Database.getOpenedDb("mainIosDb")
static let FRONTEND_DB:FMDatabase = Database.getOpenedDb("JsDb")
}
......这是一个崩溃日志:
Crashed: com.rsginc.rmove.TripStorage.singleton
0 libswiftCore.dylib 0x1008c8a18 _TFs16_assertionFailedFTVs12StaticStringSSS_Su5flagsVs6UInt32_Os5Never + 164
1 rMove 0x10022dc88 type metadata accessor for Database (Utilities.swift)
2 rMove 0x10022d320 globalinit_33_2CFFAAA7EF6073F07DB88164E80B793B_func11 (Utilities.swift:177)
3 libdispatch.dylib 0x188c6e1bc _dispatch_client_callout + 16
4 libdispatch.dylib 0x188c6efb0 dispatch_once_f + 56
5 rMove 0x10022d35c Database.BACKEND_DB.unsafeMutableAddressor (Utilities.swift)
6 rMove 0x100213864 TripStorage.init() -> TripStorage (TripStorage.swift:13)
7 rMove 0x100213508 static TripStorage.(getInstance() -> TripStorage).(closure #1) (TripStorage.swift:87)
8 libdispatch.dylib 0x188c6e1bc _dispatch_client_callout + 16
9 libdispatch.dylib 0x188c7b7f0 _dispatch_barrier_sync_f_invoke + 84
10 rMove 0x100213418 static TripStorage.getInstance() -> TripStorage (TripStorage.swift)
11 rMove 0x10020384c specialized CordovaInterface.start(Double, Double, Int, Double, Double, String, Bool, Int, Int, Double, Int, Int, Int, Int) -> () (CordovaInterface.swift)
12 rMove 0x1001fea74 CordovaInterface.configureFromDefaults() -> () (CordovaInterface.swift)
13 rMove 0x1002014fc partial apply (CordovaInterface.swift)
14 rMove 0x10020b1e0 specialized TerminationRecovery.applicationLaunched(notification : NSNotification) -> () (TerminationRecovery.swift)
15 rMove 0x10020a020 @objc TerminationRecovery.applicationLaunched(notification : NSNotification) -> () (TerminationRecovery.swift)
16 CoreFoundation 0x189d7eb10 __CFNOTIFICATIONCENTER_IS_CALLING_OUT_TO_AN_OBSERVER__ + 20
17 CoreFoundation 0x189d7e214 _CFXRegistrationPost + 400
18 CoreFoundation 0x189d7df90 ___CFXNotificationPost_block_invoke + 60
19 CoreFoundation 0x189dedb8c -[_CFXNotificationRegistrar find:object:observer:enumerator:] + 1504
20 CoreFoundation 0x189cbfe64 _CFXNotificationPost + 376
21 Foundation 0x18a7f4e0c -[NSNotificationCenter postNotificationName:object:userInfo:] + 68
22 UIKit 0x18ff1cd38 -[UIApplication _callInitializationDelegatesForMainScene:transitionContext:] + 4196
23 UIKit 0x18ff22808 -[UIApplication _runWithMainScene:transitionContext:completion:] + 1656
24 UIKit 0x18ff37104 __84-[UIApplication _handleApplicationActivationWithScene:transitionContext:completion:]_block_invoke.3139 + 48
25 UIKit 0x18ff1f7ec -[UIApplication workspaceDidEndTransaction:] + 168
26 FrontBoardServices 0x18b9bb92c __FBSSERIALQUEUE_IS_CALLING_OUT_TO_A_BLOCK__ + 36
27 FrontBoardServices 0x18b9bb798 -[FBSSerialQueue _performNext] + 176
28 FrontBoardServices 0x18b9bbb40 -[FBSSerialQueue _performNextFromRunLoopSource] + 56
29 CoreFoundation 0x189d92b5c __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 24
30 CoreFoundation 0x189d924a4 __CFRunLoopDoSources0 + 524
31 CoreFoundation 0x189d900a4 __CFRunLoopRun + 804
32 CoreFoundation 0x189cbe2b8 CFRunLoopRunSpecific + 444
33 UIKit 0x18fd057b0 -[UIApplication _run] + 608
34 UIKit 0x18fd00534 UIApplicationMain + 208
35 rMove 0x100103b00 main (main.m:32)
36 libdispatch.dylib 0x188ca15b8 (Missing)
堆栈跟踪中的这一行:
2 rMove 0x10022d320 globalinit_33_2CFFAAA7EF6073F07DB88164E80B793B_func11 (Utilities.swift:177)
与代码中的这一行对应:
static let BACKEND_DB:FMDatabase = Database.getOpenedDb("mainIosDb")
我的猜测和希望是,该堆栈跟踪中有一些很好的信息直接指出了这个问题的原因,我只是不知道如何解释它。
答案 0 :(得分:0)
你必须用旗帜打开数据库, 尝试belloe代码它适合我
[self.database openWithFlags:SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE | SQLITE_OPEN_FILEPROTECTION_NONE];