Core Data and Background Tasks

Hello, our application works with Core Data to save some datas about its activity. We have background Tasks implemented and our app execution in background shows this error message in the Logs:

error: Failed to acquire background task assertion for task 'CoreData: Executing write request'.

Anyone could explain what this message means? Could it be that NSManagedObjectContext changes might not be written?

Answered by DTS Engineer in 816933022

Assuming that the message was from the Core Data framework, the reason would be that Core Data was trying to extend the app's background execution time to do a write request by calling beginBackgroundTask(withName:expirationHandler:), and the API returned invalid, which means that the system didn't allow more background execution time for the app.

The error doesn't necessarily indicate a write request failure though, because the app may still have a big enough time window for the request and Core Data may handle the write request in the other way.

You'd likely see another error, if a write failure indeed happened. For example, if Core Data was trying to write data and the app was suspended because the background execution time ran out, you would likely see a 0xdeadlock crash.

Best,
——
Ziqiao Chen
 Worldwide Developer Relations.

Accepted Answer

Assuming that the message was from the Core Data framework, the reason would be that Core Data was trying to extend the app's background execution time to do a write request by calling beginBackgroundTask(withName:expirationHandler:), and the API returned invalid, which means that the system didn't allow more background execution time for the app.

The error doesn't necessarily indicate a write request failure though, because the app may still have a big enough time window for the request and Core Data may handle the write request in the other way.

You'd likely see another error, if a write failure indeed happened. For example, if Core Data was trying to write data and the app was suspended because the background execution time ran out, you would likely see a 0xdeadlock crash.

Best,
——
Ziqiao Chen
 Worldwide Developer Relations.

Core Data and Background Tasks
 
 
Q