Remove data from NSKeyedArchvier - iphone

I am storing data with NSKeyedArchiver. When a user logs out of my app, I would like to clear all the data that I have stored. How can I go about doing that? It doesn't appear that [NSKeyedUnarchiver unarchiveObjectWithFile:] removes the file.
Thanks!

NSKeyedArchiver & NSKeyedUnarchiver only encode and decode your objects so they can be stored and retreived. If you are storing the data in a file, just delete the file (use NSFileManager). If you are storing data in other places (eg NSUserDefaults, a database, etc) then you need to delete the data as appropriate for the store.

Related

Saving CKAsset from Core Data

I have some data like pictures, stored in Core Data as binary data and marked as "Allows External Storage". I'd like to write this data to the CloudKit. Is it possible to get URLs for this data and pass it to CKAsset, or transform somehow this data to CKAsset without double writing this data to some temporary files? Thank you.
Accessing external binary data directly is not supported and there's no API for it. Unofficially it's not hard to figure out what directory the files are stored in, but it's not useful because
Filenames are UUIDs, and there's no documented way to link a managed object to a UUID, so you don't know which file to use.
The option is to allow external storage, so there's no guarantee that an external file exists. Some instances may not use external storage.
I'm not sure what CKAsset requires but you'll have to look up the binary data via the managed object first.

Store gif in Core Data - Swift 3

How can I store a GIF image fetched from HTTP request in Core Data?
I already store my still images with UIImagePNGRepresentation as NSData in a Binary data attribute, but I have how to proceed with a gif?
Edit: What I've done so far is getting the data from the request, then storing it in the BinaryData using UIImagePNGRepresentation. But when I try to create a UIImage with SwiftyGif, an error message shows up saying Could not determine delay times for GIF. So I guess the PNG representation mess with the frames of the GIF.
I also tried to directly store the data from the http request but then the UIView throw me this error: Terminating app due to uncaught exception 'CALayerInvalidGeometry', reason: 'CALayer position contains NaN
A gif is a file format, and files are data and data can be stored in core data. Nevertheless it s a bad practice to store large amount of binary data in any database, and a filesystem is often a better choice. I would recommend storing the files in the file system and keeping relative file path in the database (make sure it is relative, because the app's container directory can change). If these are images that are retrieve from a server and can be refetched they should be stored in the tmp directory (or should just be managed separately with something like SDWebImage). If they are not retrievable later then they should be stored in the documents directory.
If that seems to hard then you can still store them in core-data. Core-data does has an option to allow it to store property as files outside of the database file ("Allows External Storage").
When you call UIImagePNGRepresentation, you're converting PNG data to a Swift Data structure. If you save it in Core Data, you'll be using a binary attribute.
You didn't mention how you're storing your GIFs. If you have them in Data instances then you do it exactly as with the PNGs-- with a Core Data binary attribute. If you have something other than a Data, please explain what kind of object or struct you're using to hold the GIF data.

Trouble working with nested JSON objects on Objective-C

On my iPhone app, I'm getting this response from the server:
I need to save this data to disk, so it can be accessed while offline, and am doing it with NSKeyedArchiver.
I need to save each id_preguntawith its correspondent values, including the array respuestas, which has some objects, too.
How can I break this server response into more manageable data, so I can save it? My current approach is using NSMutableDictionary, but I just can't understand the logic behind this (I'm way too tired).
Thanks in advance.
If you were to serialize this into NSMutableDictionary you could just write the contents of the dictionary to a file. If you need to parse the data into objects (for whatever reason) or you want the general scalability and performance of a database you may want to look at this link to -> Core Data.
However; you did not specify in what form you want the data.
Frank
See these in NSDictionary class reference...
- (BOOL)writeToFile:(NSString *)path atomically:(BOOL)flag
and
+ (id)dictionaryWithContentsOfFile:(NSString *)path`

iPhone Data Structure and accessor code advice

I need a code based solution for storing the following data without using CoreData. Preferably I would be able to access each row based on it's index, and each data field using a key/pair perhaps?
The PHP equivalent of what I'm looking for would be something like;
echo $arr[0]['Image'];
Apple.png
The simplest way is to use an NSMutableArray of NSMutableDictionary objects.
NSMutableArray has a -writeToURL: method you can use to save it to disk.
You can use either plist, sqlite or Any other data storage take look at this link. and if you are using like Sqlite than select query will get the Apple.png.

Fetching data from webservice and caching on iphone

I want to access a webservice to fetch alot of data (e.g. product lists/details/search results) and display this.
Are there any best practices for this type of operations?
Performance-wise, is there any better way than retrieving, parsing and displaying text data on each request and maybe load images in the background? Are there any wise caching policies which may be applied?
If I were doing something like this from the ground-up, here's what I'd do:
Have the web site post all the data in XML. Except for maybe the pictures - just have an XML field specify a URL for each picture. So, for example, say I was doing a product list.
Use NSXMLParser to both fetch and parse the XML data.
Use a separate NSData dataWithContentsOfURL: call to fetch the contents of each image, with the URL from the XML data
Write the XML data, (and the NSData image) to a database table with CoreData. Add a indexed timestamp field to the table.
You can now use the timestamp field to keep the newest "x" records in the database - and can purge the older ones if/when you need to.
Use the contents of the database table to populate a UITableView - or however else you want to present.
Make some sort of "next", "prev" or "update" fields in the UITableView to get more data from the web, if you need to display more data than is what is cached - or you want to update the data in the cache.