I am using background mode for downloading data from the server and storing it in Realm. I am getting weird crash reports from my users. I attached an example crash log.
My code:
private func addParseReportToDb(serverResponses: [AddNewParseReportResponse], trackedProfileRecordId: String) {
let trackedProfile = TrackedProfile.getByPrimaryKey(recordId: trackedProfileRecordId)!
for serverResponse in serverResponses {
let parseReport = ParseReport()
parseReport.biograph = serverResponse.biography
let realm = try! Realm()
try! realm.write({
realm.add(parseReport)
})
createUserFeedWithLatestReports(trackedProfile: trackedProfile)
}
}
Above method is calling from below method:
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
let realm = try! Realm()
let tst = Test()
tst.type = "silent"
try! realm.write({
realm.add(tst)
})
let parseService = InstaParseService()
parseService.downloadLatestReports {
parseService.getUsersForFetch { status in
completionHandler(.newData)
}
}
}
parse.downloadLatestReports method calls the addParseReportToDb method.
The crash point is realm.add(parseReport)
As you can see actually I can add another object before adding the parseReport without any problem. Don't know why but that part crashes sometimes.