Wrong batched fetch request behaviour from iOS 14 beta 5 onwards

On iOS 14 beta 5 and 6, when fetching a request on a new private queue context that has persistentContainer.newBackgroundContext() as a parent, returns the wrong number of objects (always 0 even if there are objects in the db).

The same code on iOS 13 and iOS 14 beta 1 to 4 returns the correct number of objects.

Am I doing something wrong?

In the following sample code the second assert fails on Xcode 12 beta 5 and 6.

Code Block swift
let rootContext = self.persistentContainer.newBackgroundContext()
let viewContext = persistentContainer.viewContext
let goodRequest = NSFetchRequest<NSFetchRequestResult>()
goodRequest.entity = NSEntityDescription.entity(forEntityName: Podcast.entityName(), in: viewContext)
let batchRequest = NSFetchRequest<NSFetchRequestResult>()
batchRequest.fetchBatchSize = 20
batchRequest.entity = NSEntityDescription.entity(forEntityName: Podcast.entityName(), in: viewContext)
let entityDescription = NSEntityDescription.entity(forEntityName: Podcast.entityName(), in: viewContext)!
let newPodcast = NSManagedObject(entity: entityDescription, insertInto: viewContext) as! Podcast
newPodcast.title = "The Title"
if viewContext.hasChanges {
try? viewContext.save()
}
guard
let goodPodcastsAfter = try! viewContext.fetch(goodRequest) as? [Podcast],
let batchPodcastsAfter = try! viewContext.fetch(goodRequest) as? [Podcast]
else {return}
NSLog("After adding new podcast - podcast count - good request: \(goodPodcastsAfter.count) - batch request: \(batchPodcastsAfter.count)")
assert(goodPodcastsAfter.count == batchPodcastsAfter.count, "Count of fetched podcasts should match")
let newContext = NSManagedObjectContext(concurrencyType: NSManagedObjectContextConcurrencyType.privateQueueConcurrencyType)
newContext.parent = rootContext
newContext.automaticallyMergesChangesFromParent = true
newContext.mergePolicy = NSMergePolicy.mergeByPropertyObjectTrump
newContext.perform {
guard
let goodPodcastsInNewContext = try? newContext.fetch(goodRequest) as? [Podcast],
let batchPodcastsInNewContext = try? newContext.fetch(batchRequest) as? [Podcast]
else {return}
NSLog("In new context - podcast count - good request: \(goodPodcastsInNewContext.count) - batch request: \(batchPodcastsInNewContext.count)")
assert(goodPodcastsInNewContext.count == batchPodcastsInNewContext.count, "Count of fetched podcasts should match")
}


Replies

...addendum: the issue happens only for requests that have a fetchBatchSize set.
FYI NSEntityDescription.entity(forEntityName: Podcast.entityName(), in: viewContext), can be replaced with Podcast(context: viewContext) initializer since iOS 10

Submit bug report to feedbackassistant.apple.com and post any updates here
Unfortunately an issue exists in seed 5 & 6 with a child managed object context and fetchBatchSize. This is resolved in the upcoming seed 7.
Bug submitted as: FB8562978
Happy to see Apple engineers are already aware of this.
I've updated my iPhone to iOS 14 beta 7, but this issue still keeps happening.
You can verify this on beta 7. If you are still having issues, please file your own report and post the FB number here. Thanks
I've reported this issue which is FB8587463.
Currently it's so unique.
This issue has been resolved on specific device but not on other specific device.
  • iPhone Xs : Resolved.

  • iPhone 7 : The issue is still occurring.

Both of them are using 14 beta 7.
Please take a look at that issue.

Thanks in advance.

Regards,
Seunghwan.