0

I set the NSURLSessionDownloadTask in NSMutableDictionary. Then I want to save this dictionary in NSUserDefaults.

I know that I can't save custom object in NSUserDefaults and, after searching, I've found this answer: How to store custom objects in NSUserDefaults, but I couldn't implement to my code.

Here is part of my code:

[self.downloadTasksDictionary setObject:dlTask forKey:[NSString stringWithFormat:@"%@",customId]];

[[NSUserDefaults standardUserDefaults] setObject:self.downloadTasksDictionary forKey:@"DownloadTaskDict"];
[[NSUserDefaults standardUserDefaults] synchronize];

Is there any way to save this NSMutableDictionary in NSUserDefaults?

Thanks.

Community
  • 1
  • 1
Kirdok
  • 1,904
  • 2
  • 22
  • 27

1 Answers1

0

You need to use NSKeyArchiver for this. Take a look at this SO post: Save custom objects into NSUserDefaults and Why does NSUserDefaults fail to save NSMutableDictionary?

But, why not saving request descriptor instead of downloadTask object?

In my project I have a NetworkManager which creates a NetworkTask from a NetworkRequest which is a struct. This struct can be created from a json. It works really well.

Community
  • 1
  • 1
thierryb
  • 3,660
  • 4
  • 42
  • 58