Design pattern - duplicate objects in Realm to generate static data - swift

I’m a fresh starter with Realm Swift. I did some sort of Realm graph object (seems to work for most people) and a UI mockup to explain you what is the problem with my current design.
An example of the UI workflow
The user accesses a list of workouts he created and selects one.
The user starts a training session based on the selected workout.
The user finishes the workout and then gets the summary page.
Later, the user can access a list of past training sessions.
The user can select a specific session to see how it went.
Realm graph object representation
My current issue
My problem with the current graph model is related to the 'TrainingSession object' where I need static data to generate charts and statistics.
The WorkoutSession object has relationships with objects which contain dynamic content, since I can do the following:
The user can delete at anytime a workout or change its name, etc
The user can add or delete an exercise within a workout
By changing a workout or its nested objects (exercises, sets, etc), it will change the information displayed in a training session (screen ⑤). E.g.:
Remove pushups in screen ② for Workout A.
Check a specific session with Workout A in screen ⑤, then pushups will be removed.
Approaches
I really don’t know how to deal with it? As you may guess, I’m trying to reduce data duplicates, so I came with solutions such as:
Duplicating all related objects to the workoutSession (Workout, Exercises and so on), but if I do that each time I create a session, it’s going to be huge.
Creating some sort of workout/exercise versioning system, so the training session always refer to a specific version of the workout/exercise. A workout is actually working like a blueprint.
I hope my visual explanations are helpful enough. If you feel you need me to add some realm code, I’ll do it (but I can’t share the project I’m working on).
Thanks in advance for your precious help ;)

Related

What's The Best Way To Collect A Lot Of Information From User?

I'm just learning Swift and IOS development. I have an issue, I'm creating an app that need's to get a lot of information about a product, from the user. There will be many fields the user needs to fill out about this product.
At first, I just added some labels and below each label a text field for the user to fill in information for each label. However, I found out that I quickly ran out of room for my labels and text fields.
So my question is What would be the best Object to use to collect all this information. Also, this information will be saved to Realm database, and each product will then be displayed on a table view when the app opens. Then when the user clicks the product it will show them all the stored information about that product. Also when they click the + in the navigation bar it will let them add a new product and all the information for it.
So that is how my app will function. Any help on the best way to collect and display this vast amount of information would be very helpful to me.
Thanks
I think the best way is to categorize the information and show the input fields of each category in a separate page.
It depends of which information you want. IOS development is not like Android, that you can call an Intent to access almost everything in device. but, with Custom URL, you can access address contacts, calls, messages. You can access the photo library, capture geolocation. Apple is much more restrictive then Google.

Parse.com Database design for likes/comments`

I am working on an application which will have users.. who create posts.. and other users can like/comment on any post.
I am trying to figure out a best way to design db tables for this. I have read the anypics tutorial on parse.com site. They keep all comments and likes in a table called "Activity". (which makes sense) being able to query any type of activity (like/comment) from a separate table without having to touch "posts" table.
My question is- in this scenario how do I fetch all posts that current user created along with likes and comments on each those posts?
Anypic app by parse makes a separate request to fetch number of likes on each post (which I think is not ideal.) I am new to nosql data stores.. so if someone could help me out with suggestion on how to structure data that would be great.
Also, how bad is it to store all likes/comments as an array in the post itself? I think this won't scale but I might be wrong.
Thanks
In terms of Parse, I would use an afterSave Cloud Function to update the Post anytime a like/comment is added.
Have a look at the documentation here, in the most simple case you just create an afterSave for the Activity class that increments the like/comment count.
In a more advanced scenario you might want to handle update/delete too. E.g. if someone can change their 'like' to 'not like' you would need to look at the before/after value and increase/decrease the counter as needed.
I am a fan of storing extra 'redundant' data, and no-sql/document-db systems work well for that. This is based on the idea that writes are done infrequently compared to the number of reads, so doing some extra work during/after the write has less impact and makes the app work more smoothly.

Entity Framework Deep Copy

I have a web application that allows the clerk to edit information. I copy an entity before the edit starts in case the user decides to cancel the changes. The problem is that any changes made on the copy is applied to the original object.
In C# I would create a deep copy to avoid that issue, but this application is using Entity Framework... I am not sure how to do a deep copy of an entity.
Here is more details on my problem... I am still trying to resolve.
I have a xaml screen with a grid binded to a list of inventory items. The items are an EntitySet. When I want the user edits one of the items, I copy the values of the current entity in an object "EntityToEdit" of the same type. The user makes a change, saves, and the list is automatically refreshed with the changes.
The problem occurs when the user selects another item to edit. That second item is somehow changed with the changes made on the first item....
How can I break the "link"?!?
There is a lot of stuff going on in your question, I barely know where to start.
First, I do not recommend binding the Entities directly to your UI. Instead I recommend using some form of abstraction between your database and the presentation layer of your application.
There are a couple of established best-practices patterns that could be used, mostly depending on what technical environment you are working in. Have a look at the MVC pattern (Model-View-Controller) or at MVVM (Model-View-ViewModel).
Regarding your deep copy, there are lots of possible solutions. You can find some here at StackOverflow, like this one: "How do you do a deep copy an object in .Net?", where the objects are serialized into a Stream. Altough I, too, was using this kind of deep copy, but I try to avoid this. I see only very rare cases where a deep copy is really needed.
You should also consider implementing the [IEditableObject][4] interface on those objects that you want to edit. This allows you for an easy way to structure when and how your edited values are commited and to optionally or cancel or reset your edits by implementing straight forward methods like BeginEdit(), EndEdit(), and CancelEdit(). Some .NET controls that support this interface out-of-the-box and call these interface methods automatically if they exist on your objects.
By implementing IEditableObject you have full control of how the values are committed to which object. This will help you to avoid changing the original object accidentally.

Yelp API displaying data

I've been looking up and down and there doesn't seem to be much documentation on this. I am just beginning development, and have no prior programming experience aside from HTML which doesn't really count.
I am able to successfully pull data with URL queries as I can see in my console. My question is, if I wanted to send a query for restaurant reviews, as an example, how do I get that data from the console to the iPhone view, right on the screen? If someone were able to provide a simple example I would appreciate it.
I'll give you the general steps:
1) Parse the JSON (which is probably an array of dictionaries) so loop through the array with an inner loop iterating the dictionaries. You could also use JSONKit.
2) Optionally cache the data somewhere (NOTE: If you use core data, there is a nice NSManagedObject method that will basically create an NSManagedObject directly from JSON).
3) Display it using UIKit (i.e. a Table view, where each cell displays the text and images your are retrieving).
If you need any specific help with any of those steps, please post new questions as you go along -- Break it down and tackle one problem at a time.
Also, if you use Core Data to persist this JSON data, you could use the NSFetchedResultsController as the datasource for your table view, which simplifies that some too.
I know you probably wanted me to write the code for you, but you've asked a very ambiguous question, whose scope is really too broad. If you follow those steps and have specific questions, we will help you work through it as you go.

MongoDB permissions-based modelling problem

I'm trying to model a simple, experimental app as I learn Symfony and Doctrine.
My data model requires some flexibility, so I'm currenty looking into the possibility of using either an EAV model, or document store in MongoDB.
Here's my basic requirements:
Users will be able to store and share their favourite things (TV prog, website, song etc).
The list of possible 'things' a user can store is unknown. For example, a user may want to store their favourite animal.
Users can share their favourite things with other users. However, a user can decide what he / she shares with each other user. For example, a user may share their favourite movie with one user, but not another.
A typical user will log in and view all the favourite things from their list of friends, depending on what his friends have decided to share. The user will also update their own favourite things, which will be reflected when each other users views their own profile. Finally, the user may change which of his friends can see what of his favourite thing.
I've worked a lot with Magento, which uses the EAV model extensively. However, I'm adding another layer of complexity by restricting which users can see what information.
I'm instantly drawn to MongoDB as the schemaless format gives me the flexibility I require. However, I'm not sure how easy (or efficient) it will be to access the data once it's saved. I'm also concerned about how changes to the data will be managed, e.g. a user changes their favourite film.
I'm hoping someone can point me in the right direction. This is purely a demo app I'm building to further my knowledge, but I'm treating it like a real-world app where data access times are super-important.
Modelling this kind of app in a traditional relational DB makes me sweat when I think about the crazy number of joins I'd need to get the data for one user.
Thanks for reading this far, and please let me know if I can provide anymore information.
Regards,
Fish
You need to choose a model based on how you need to access the data.
If you just need to filter out some values when viewing the user profile, a single document for each user would work quite well, with each favorite within that having a list of authorized user/group IDs that is applied in the application code. Both read and write are single operations on a known document in this case, so will be fast.
If you need views across multiple profiles though, your main document should probably be the favorite. You'll need to set up the right indexes, but performance shouldn't be a problem.
Actually, the permissions you describe don't add that much complexity to an EAV schema - as long as attributes can have multiple values the permissions list is just one more attribute.