I use back4app as my backend environment which is almost similar to Parse.
I would like to ask a few questions regarding organising data models relationships. Let me add a few words about my structure:
I have a User data model and a Project data model. Project can contain many User records. When I want to put a user to a project I wrap it to another data model called ProjectUser. I need this for separating levels when a User can be a part of entire system and when a User can be just a part of smaller things such as Project component I've described.
ProjectUser class has a User and Project as Relation (not sure do I need to use Pointer here):
So now looks like I can filter ProjectUser using Project key and get all needed users in appropriate project.
My question do I need to use such approach with filtering or do I need to add a new column to Project class with Array type and append this array every time I put new ProjectUser to a destination project? Can my Project class just contain array of another custom classes?
To summarise:
Do I need to use pointers instead of relations?
Is this better to create additional object with two custom classes in it (ProjectUser) or it's ok to use an array in Project data model to retrieve all Users or ProjectUsers.
UP
Faced the same question
have back4app as backend ad table to filter by related object (User).
Related
If I create models for request and response parameters for every http request in iOS project, it is easier for me to deal with models rather than dictionaries.
However, it will create too many models.
Is this a good approach or not?
And during the development, I found if there is only one model, for example, in an online shopping system, I only have one store model. But I use it in store module, cart model and order model. Actually in each module, the different attributes of the store model has been used. So this giant model always has some extra attributes for usage in each module.
Is this a good approach to manage the models in iOS projects? Or should I create CartStore model, OrderStore model?
To me, you should create models for different flows/modules to adapt Single Responsibility Principle.
From your shoes, the Store model should only contain the attributes of the store, if Cart or Order module need any further attributes then you should create another model.
Hope this helps.
The model approach will save a ton of your time when come back to fix or add on a new feature. Sure, They will take quite time but today Apple has already provide code codable/decodable protocol which will help you to handle parsing between JSON and Object Model. Also writing a unit test or even integration test it will be much easier for you.
Base on which data response of request, if have the same data structure should only create 1.
Ex: api get all stores and api get store with condition (distance, rating, ...) return storeModel, then we just using only 1 storeModel. And we have api get store detail (this api return more info than api get list stores above) just add more property in storeModel and using it again. So that with 3 apis, we only using storeModel
With your example, I think should create StoreModel, CartModel and OrderModel inherits from BaseModel. In this BaseModel, we'll have the all property which StoreModel, CartModel and OrderModel have, and in each children model, we will have only specific property.
Hope this help!
I have to create and remove attributes based on an api response in Objective C.
For example, Now my api response contains fields "facebook", "whatsapp" and "viber". But in future the reponse can add "youtube". Based on this response, I have to remove all the attributes and values of an entity "Social", and create Four attributes now and set values.
How to do that programmatically? Because the default *.xcdatamodeld file cant help me here, right?
Note: My project is in objective C.
The data model is mutable when the app starts-- you can completely build the model in code, and not use the model editor, for example. But as soon as you load a persistent store file, you must treat the model as fixed. Any changes after loading a persistent store will cause crashes. That means any changes would have to happen before calling either loadPersistentStores(completionHandler:) or addPersistentStore(with:completionHandler:).
Alexander's suggestion of optional attributes is a good one. If you need the model to be more dynamic, you would need to create a new related entity which would store the service name plus whatever information you need to save about the service. If you did this, your Social entity would have a to-many relationship to a new entity called something like Service. Service would have a string property called name that would have values like twitter, facebook, youtube, etc. It would also have whatever other attributes you need to save about the service.
You can create all 4 fields in advance and just make them optional and fill them depending on the server response. But you cannot add new attributes in runtime. Your *.xcdatamodeld file compiles into *.momd and it contains all the data to create tables in the DB since Core Data by default works with SQLite under the hood and it's a relational database management system.
To make attributes optional you should check that.
And then newly created objects contain nil as default values of object properties. So, in your case your "youtube" property of Social object will be just nil.
Overview:
I have an iOS app that uses CoreData
There is an entity called Animal
It has a set of records Lion, Tiger and Elephant
I would like to mark only one of the records as favourite.
Similar entities with the same approach:
Similarly I could have other entities such as Car, Bike.
Each entity would have a set of records.
Again each entity should only have one favourite record
Reason:
App has an option to create a new document
When the new document is created, it would be populated with default values for each entity (by selecting the favourite record of each entity)
Note: Only one record can be marked as favourite at a given time
Possible models I thought of:
1. Field called isFavourite
Create a field in Animal called isFavourite.
Mark only one of the rows as isFavourite as true.
Much of the logic to maintain isFavourite is managed in code.
2. Separate entity called Favourite
Create a separate table called Favourite and have a dummy row in it.
Establish a relationship from Favourite to Animal called animal.
This will point to the favourite record.
Questions:
What is the preferred approach to tackle this problem ?
Are there any other alternatives ?
Go with option 2, maybe call it Config. If you want to ensure it is just a singleton add a attribute that is unique and can only be zero.
You can write a helper computed var returning true if the reverse relationship is non-nil.
Main advantage of option 2 is the simplicity of changing the favourite, you don't have to scan through all the items to to set them non-favourite just change it on the singleton config.
Give some thought to other parts of the app and to what you might want to do in the future.
Adding a field: Works OK but requires some code to maintain, which might be error prone. On the other hand maybe one day the app might allow multiple favorites, and this will just work with that.
Using a separate entity: Also works OK but adds a whole new entity where you'll only have a single instance. In general, if you have an entity where you only ever want one instance, you're doing it wrong. On the other hand this also works well with the potential for multiple favorites.
A third approach is to save the objectID for the favorite animal somewhere outside of Core Data, like UserDefaults. Save it, and then find the favorite by using NSManagedObjectContext's existingObject(with:) method. You can't save the NSManagedObjectID directly but you can get its uriRepresentation() and save that.
I'd probably go with #1 in most cases but it depends what else I need in the app.
I am struggling with something I think should be basic but cannot figure out. I have two entities in core data with a one to one and one to many relationship. They are Company which can have multiple Opportunities.I want to load a table view listing the opportunities (sorted by name) with their associated companies. Can this be done by simply accessing the Opportunity entity? If so, how do I access company? The Opportunity class references it as a "Company" type and so I tried to go using dot notation through to company.companyName but it failed on that, and if I change it to simply company (of type Company) it does show .Company: and other reference data but not the simple name field I am looking for. This seems as if it should be simple but...........
This was simple and I was overlooking the ability to load the fetchedresultscontroller with the right type (in my case the Opportunity class) and then use dot notation from there. I was trying to do it with key value access which did not work. Cheers
I'm trying to have multiple copies of a single product objects in different view controllers (i.e. you have a product in a shopping list and when you search queries web-service and returns searched products). There is a symbol on search tableviewCell if that product is in the shopping list.
I thought of two ways to do this:
Have an array in a singleton class that caches the products in a NSMutableDictionary by their id numbers and every time a products is created it check to see if there is one in its place. If so it just uses the product already there. I can have a setting in the product that states if it is on the shopping list or not.
Use core data. I tried to implement it but not sure how I can exactly do this. I was thinking of using core data so that when i update a product object it is also updated in other parts of the app using NSFetchedResultsController.
What is "standard practice" for this situation?
Let me know what you think and how you would approach this. Thanks!
You can use singleton pattern for your data manager class, which will hold your data in an array of Models (For e.g.: Product)
If you use singleton data manager (for e.g.: ProductManager) then your data (for e.g.: Product entities) would be persistent for all your class files in application life cycle mode.
Here you can find more information:
Objective C Singleton Class
http://www.galloway.me.uk/tutorials/singleton-classes/
Hope this is what you are looking for.
if the shopping list doesnt need to be written to disk, option 1 is fine. if it does need to be written to disk, you can use core data.. or use sqlite, or NSArchiver. there are a few ways of storing data.