Chapter 4: How to 'cancel the fetch request with NSAsynchronousFetchResult’s cancel() method.'

In this chapter, you mention as an aside that it is possible to cancel the fetch request with
NSAsynchronousFetchResult’s cancel() method. I can’t figure out, however, what the NSAsync…Result value is. Could you please provide a code example of how to cancel the request from within the do try block?

1 Like

I am just working my way through this as well but the way to do it which I found was casting the result to a NSPersistentStoreAsynchronousResult, so for example

  let result = try coreDataStack.managedContext.execute(asyncFetchRequest) as! NSPersistentStoreAsynchronousResult
  result.cancel()

Or you can actually cancel the request from within the closure itself, the best way to do it is to keep track of the progress and if some condition comes to pass or its taking a while is to cancel it I guess,
There is a good blog on it here: Asynchronous core data requests

1 Like