Hello everyone!
Iāve been developing in Swift for about a year. Iāve learned a lot trough the tutorials here and just by doing my own researches but I still believe I have a lot more to improve as a developer. Iāve done many things but Iāve never had someone to actually check my coding and discuss it with me, or someone to discuss about the daily challenge, I just kind of did the way it āworkedā. Do you guys have any suggestions for me so I can validate the work Iāve been doing? I mean itās working but maybe not in the best way.
Thanks !!
What do you think about bringing your examples here, to discuss? It seems like a pretty good format to me.
OK, sounds good to meā¦Iāll use this post for the first question since I canāt create a topic at the General Discussion areaā¦
So, I have an app that Iām buildingā¦In this app I receive one JSON that I will pretty much populate my whole app with information from it. Iām using Alamofire for the requests and SwiftyJSON to manipulate the data. Iāve generated the classes trough JSONExport, and after the http request I just create the classes that Iāve generated with SwiftyJSON, set them in the NSUserDefaults and then Iāve created a singleton of my main class that would try to get the information from NSUserDefaults and return it if I donāt have internet connection or if occurred any error.
1-Is it ok to generate classes trough this app?
2-Is it ok to set in the NSUserDefault the whole JSON that I received in the first request? I know that NSUserDefault is usually used for things like User Preference but is it worth usign SQLite? Core Data? Wouldnāt that make it a lot more complex?
3-Does it make sense to create a singleton of the main class that gets the information from another singleton ? How should I do that? (if Iām not mistaken, NSUserDefault is a singleton that persists right? )
4-Iāve made my singleton from the main class an optional so if I canāt get the information from NSUserDefault I just return nil and inside the ViewControllers where I use it i just do a
āif let variable = singletonFromMainClass
{
self.variable = variable
}ā
Is this the best approach?
thanks once again.
- I donāt know, Iāve not used JSONExport, but if itās taking a JSON file and making a struct template or something then it canāt hurt to give you a starting point.
- You can put a data structure into NSUserDefaults, but personally I would probably try to save it into a file in a data directory.
- NSUserDefaults is a class; its āstandardUserDefaultsā item is usually the one used, and is usually thought of as a singleton, but you can have more than one. I would avoid using a singleton where possible (unless itās for something like the UIApplication delegate, which there really should be only one of). There are other ways to structure your app to pass data around; limiting the number of types which can access your information reduces the number of places you have to check if something accesses it incorrectly.
- Your code example implies that
self.variable
is an optional. You might as well just type self.variable = singletonFromMainClass
, if thereās no other need for your variable
variable.
As your third question leads to the bigger questions more directly: after the basics, I would look into app architecture and design patterns. Improving your appās APIs through its data encapsulation can make it more predictable and less error-prone - especially if you also write unit tests to prove it all works.
Thanks for the reply.
2- What you think about keeping it in cache? Where I work my supervisor told me to do so, and Iāve been looking into it and found this reference which looks like a very good approach for the purpose.
3- Any references on how to pass data around my app? Letās say I have a structure like:
School ā Course ā Subjectā¦ Inside the JSON I receive an array of Schools with their Courses and inside the Courses the Subjects of that Courses. In some ViewControllers Iāll use information about the School, and in the next VC info about the Course etcā¦ You would save those informations in separate files ? I canāt visualize another way to limit the access of types with that structure.
4- self.variable = singletonFromMainClass.array ?? []
My singleton is an optional but my variable isnāt ā¦Would that be a better approach?
Awesome! Great help, thank you very much!