Weird crash in background mode with Realm DB

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.

Consider this from your crash report:

Last Exception Backtrace:
0   CoreFoundation   __exceptionPreprocess …
1   libobjc.A.dylib  objc_exception_throw …
2   Insta Spy        RLMAccessorContext::createObject(objc_object*, realm::CreatePolicy, bool, realm::ObjKey) + 1876 (RLMAccessor.mm:1097)
3   Insta Spy        RLMAddObjectToRealm …
4   Insta Spy        partial apply for thunk for @callee_guaranteed () -> (@error @owned Error) …
5   Insta Spy        Realm.write<a>(withoutNotifying:_:) …
6   Insta Spy        specialized InstaParseService.addParseReportToDb(serverResponses:trackedProfileRecordId:) + 1068 (InstaParseService.swift:108)

Frame 6 is your code calling into your third-party database. Frame 2 is that database throwing a language exception. You need to uncover that meaning of that exception:

  • If you can reproduce the problem locally, you will find a description of it in the Xcode console (if running inside Xcode) or in the system log (if running from the Home screen).

  • If you can’t reproduce it locally, I recommend that you ask your database vendor what exception is thrown at RLMAccessor.mm:1097.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

Weird crash in background mode with Realm DB
 
 
Q