I want to save URLSessionDownloadTask in core data when app is gone in closed state or my download state is changed e.g from waiting state to downloading state or to completed state.
All other attributes of my custom class are stored perfectly but app crashes when it stores download task.
reason to crash is
[__NSCFLocalDownloadTask encodeWithCoder:]: unrecognized selector sent to instance 0x7ff189f181c0 -[NSKeyedArchiver dealloc]: warning: NSKeyedArchiver deallocated without having had -finishEncoding called on it. Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFLocalDownloadTask encodeWithCoder:]: unrecognized selector sent to instance 0x7ff189f181c0'
this is my class
class VideoDownloadModel : NSManagedObject {
@NSManaged var videoID : NSNumber?
@NSManaged var vid : Video?
@NSManaged var downloadTask : URLSessionDownloadTask?
@NSManaged var downloadStatus : String?
}
storing it like this
let request = NSFetchRequest<NSFetchRequestResult>(entityName: (COREDATA_ENTITY_Description?.name)!)
request.returnsObjectsAsFaults = false
request.predicate = NSPredicate(format: "videoID == %@", videoModel.videoID!)
do {
let result = try COREDATA_CONTEXT.fetch(request)
print(result)
var vidArr = result as! [VideoDownloadModel]
if vidArr.count != 0 {
vidArr[0] = videoModel
COREDATA_MANAGER.saveContext()
}
} catch {
let fetchError = error as NSError
print(fetchError)
}
when URLSessionDownlaodTask is nil it works fine but when any download is started it crashes on saving.
scenerio :
I initialized my custom class object with all attributes but set task to nil.
I store that object in core data it saved perfectly.
I initialize the task of that object the download work perfectly.
Then i update the object in core data while updating the app got crash because URLSssionTask is not inheriting from NSCoding. so it don't have encoding and decoding methods.
I want some solution to solve this issue
Any help will be appreciated. Thanks.
You can't save the
URLSessionDownloadTaskin Core Data because-- as you mentioned-- it doesn't conform toNSCoding. In many cases the answer would be to write your own code to convert to/fromDatabut that doesn't work in this case. AURLSessionDownloadTaskcan only be created by aURLSession, so you can't serialize and deserialize the task object.That doesn't really matter though because saving and restoring them doesn't make sense. A
URLSessionDownloadTaskrepresents something that is in progress while the app is running. If your app is closed, that activity ends. Restoring aURLSessionDownloadTaskafter the app closes and relaunches doesn't make sense because there's no activity for it to represent. Basically, even if you could store the task object and restore it, it would be useless after restoring. There's no reason to bother.If your interest is that you want to resume incomplete downloads, you'll have to start over from the beginning. Create your
URLSessionand then use it to create a newURLSessionDownloadTask. If your interest is in getting information about a background download, you can use the session object withgetTasksWithCompletionHandler(_:)to find out whether they completed.