WKProcessPool can be permanently stored using NSUserDefaults or something else? - ios

Thanks to the bellow question's answer, I figured out how to use Cookies between multiple WKWebView by using WKProcessPool.
Cookie sharing between multiple WKWebViews
And then I'm now trying to permanently store the WKProcessPool object (of my singleton object) by using NSUserDefaults.
I've got the following error.
Attempt to insert non-property list object
So I tries to fix the error by converting WKProcessPool object into NSData by NSKeyedArchiver.
But It cannot be done because WKProcessPool does not implement encodeWithCoder: and init:aDecoder.
How can I solve this problem to store WKProcessPool object permanently?

You need to implement NSCoding for your custom objects. Here is Ray Wenderlich's tutorial
Edit:
I really didn't have idea that WKProcessPool is in built class, since apple doesn't implemented the NSCoding in it and I can't even see the public properties of this class, so even if you subclass it, you don't have any idea which properties to encode/decode.
You need to find out other solution instead of archiving/archiving the objects. Even if you achieve something using category I'm afraid that you will get the expected result.

Related

Convert Objc's data object to Dictionary which belongs to GoogleSignIn SDK

I have recently integrated GoogleSignIn in my app which upon successful authorisation returns GIDGoogleUser which is their own interface, written in Objc (and I have never worked in Objc) and I wanted to convert it to dictionary (without manually writing any parsers) so that all the profile data that is coming from Google can be sent to the backend as-is. Is there a way to do so?
You should use the method described in this thread:
How can I iterate over a class keys in objective-C?
When iterating over the properties you can store them to an NSDictionary which can be accessed in Swift in the same manner as a native Swift dictionary.
Since some of the properties are custom objects, you will probably need to use recursion.
In my opinion it is easier to fetch only the properties that you want instead of iterating over everything. It would require much less code and it is much simpler.

How to use subclass of NSManagedObject?

I'm working on a quite large app on iOS. Up until now, I've been using CoreData like this:
Have a class, that have methods like -(NSArray*)getAllEntries, or -(void)saveEntry:(Entry *)entry, and Entry has a few properties like strings, dictionaries, arrays of other objects that might or might not be saved in CoreData, etc. Mostly, I init Entry with its default ctor, and set values to properties by fetching values from the NSManagedObject by using valueForKey: I get from the CD store.
I started coding like this because at the time I was new to obj-c and I come from a C++/Java background.
Now, I'm working on a new module in the app and want to do everything the obj-c way.
As I understood, if making the Entry object a subclass of NSManagedObject, I could only init it using [NSEntityDescription insertNewObjectForEntityForName:#"Entries" inManagedObjectContext:context];, which means it would be tied to the entity? The thing is, that I might want to for example init that object from the data that I pull from the internet and I might not want to save it to the persistent store. Or, I might want to fetch the object out of the store, edit the values, but not save it to the store. Everything would be fine (probably), but as I understand, if I call the save method on the context which was used to instantiate the object, the object will be saved to the store that I didn't want to be saved.
So now I'm a little confused on how should I continue doing this. Is my old way of doing Core Data Ok, or should I use the subclass of NSManagedObject and use some tricks that I don't know of yet? And if the latter, what are those tricks?
Link answers are discouraged, but you really want to read Apple's extensive documentation on Core Data. Specifically, google for "Creating and Modifying Custom Managed Objects".
(Currently found at this link.)

Writing a PFObject to disk

When I looked up how to write a PFObject to disk, I found some quite complicated stuff.
The simplest option I found was to write encoding and decoding functions and then use NSKeyedArchiver (which I have not tried yet because I want to make sure I'm using the most adequate solution).
More complicated options involved third-party libraries so as to sync with Core Data.
Parse.com engineers said they are working on features that will make offline usage work better, but nothing is there yet AFAIK.
However, the PFUser class has a currentUser method with the following description and signature:
/*!
Gets the currently logged in user from disk and returns an instance of it.
#result Returns a PFUser that is the currently logged in user. If there is none, returns nil.
*/
+ (instancetype)currentUser;
It seems that whatever is in this method should be a good way to work with a PFObject on disk.
Is there any way to know what the technology behind this method (and behind all the logIn*methods in which the saving must be happening)?
Apparently PFUser.currentUser() actually just returns a static class variable, as seen here.

Best way to preload nsmanagedobjects

i want to store some managed objects as to make searching later on easier, what is the best way to do that? (So that it survives exiting the app)
I am thinking user defaults (but im not sure if you can store managed objects like this)
Or some sort of cache?
SO, although I am not sure this is the best way to do it, here is how I fixed this issue. I was looking for a way to preload a list of results that would persist through launches.
To accomplish this I created an NSMangedObject, called it HistoryObject, and added a relationship (to-many) to the object I wanted to store. I added those objects to this object (basically an NSManagedObject array of objects), and retrieve this object whenever I want to load the subObjects...

Use of Core Data in iOS

I have been through a couple of these answers here, but I don't think I get it right..
I have several NSArray made of JSON requests. I want to store everything in the app instead of requesting the data all the time, and I understand I should use Core Data for this.
The problem is, I don't know how to initialize this.. I have tried to read up, but I have realized it will take a long time to understand this by just reading the class reference etc.
I have added an .xcdatamodel and created an entity with attributes identical to the data in one of the json object. How can I access the file for extracting and inserting information? I plan on parsing my whole json object into this file, but how can I instantiate the entity? Which delegates and where?
All tutorials I have watched had an option when they created the project, like "Use Core Data" or something, which when checked created lots of code automatically. I don't have that..
You might wanna go through some SO links: here. Also, I once remember going through this guide for adding Core Data to my project. It might help you to go through this link for saving JSON to Core Data directly. I recommended this link here in SO a couple of times. Trust me, all this pain you are enduring setting up Core Data is well worth it when you see things starting to work!
You need to add the core data stack. You can create a new, core data project, and get the core data elements added to the app delegate specifically for core data. You will have 3 properties, and one method. Just copy/paste the declaration and implementation of these elements to your app delegate. Make sure the managedObjectModel method and the persistentStoreCoordinator method are using your actual model name.
To work with core data, you need to read the core data documentation:
http://developer.apple.com/library/mac/#documentation/cocoa/Conceptual/CoreData/cdProgrammingGuide.html
You will have to create entities to represent your data, the properties for the entities, and so fort. Get started with your project, read the documentation, and get started. You will have more questions which you will either ask or find here, but at least you got enough to get started.

Resources