save new and edited table values into core data - swift

i searching for the best way to realize the following situation:
i have data in my core data.
i open the view controller with my tableview
this tableview fetchts the core data values
but now the questions:
how should i solve the situations for new values and edited values:
Situation 1:
i can add a row with a button and fill it with data
Should i directly save it into core data, each time if a row is added?
Situation 2:
if i edited an exist row, should i directly update the exist record?
At the moment:
I request the records and save them into an array.
and all new changes will be apply to the array.
if the view will disappear, i delete all the requested records in core data and save the array value into core data. is this a good way? i think not ...

Related

Swift: Core Data - Resolving a logic issue with object creation

So, my Swift app allows a user to choose sports teams to see historic match information for. Currently, a user selects team(s) and the JSON data file of historic matches is scanned.
If a historic match includes a name of a selected team, the details of the match are stored in a Core Data entity, which is fed into my main Table View.
However, this presents an issue I can't get my head around solving.
If a user selects team A and B, and the database contains a match where team A and B played EACH OTHER, two objects for the match details are created, and as such, Table View cell is created twice, once for team A being found in the instance of the match, and again for team B.
Is there an easy and efficient way to trim any duplicates caused in this way? I don't know whether to handle this at the object creation time, or just to find a way of removing any duplicated cells from my Table View.
Thanks so much.
I think you should redesign your setup. Have all the records to be searched stored in Core Data.
If you have a hardcoded JSON file - import it on first start. If you have retrieved JSON - insert / update the elements that are new / changed in your Core Data object graph.
You would have a Match or Game entity and it would be retrieved only once. The fetch predicate would be something like
NSPredicate(format: "homeTeam = %# || guestTeam = %#", selectedTeam, selectedTeam)

Updating table content using SQLite?

Friends, here is my problem: I populate a table in Xcode using SQLite database. Here is what I want - If my table has 20 rows and all of them are populated, how to update the table if new data is saved in my SQLite database? I want the new information to overwrite the old one. How to do that in Xcode, any ideas? Thanks in advance! I am not sure if the info that I provided is enough, so let me know if you want some code or other stuff.
The key to what you're doing is in the routine you furnish for your table: cellForRowAtIndexPath. Basically, you can write that routine to furnish whatever data you want it to furnish. Once you know that your data has been updated, you can cause the entire table to refresh with a call to reloadData.

NSDictionary as a data structure for objects question

I just wanted to make sure I don't need Core Data for a simple example. If I have a UITableView where the user can hit the plus sign to add new table entries, and each entry corresponds to an individual timer event, do I store each object added to the table in an NSDictionary? Thanks.
NSMutableArray would be a better choice. An array is an ordered list, just as a table is. A dictionary is not ordered, so it's not a good match for a table.
If that's confusing, try thinking about it in concrete terms. Imagine that you have a table and you've stored the data for the table in a dictionary. At some point, the table will ask it's data source -- probably your view controller -- to provide a cell for a particular row of the table. How do you figure out which entry of the dictionary corresponds to that cell? On the other hand, if you use an array, the mapping between table rows and data items is simple.

Refreshing table data that is retrieved dynamically from JSON

I am retrieving the Json data from an URL into a table view. The problem is that i couldnt replace the old data that is retrieved, with the new data when i click another table cell. The new data is getting added to the last cell of the table cell while the old data is also getting displayed. What could be the problem...
You have to replace the data for the current cell with the new data then call the reloadData method on your tableview.
I mean you need to change the data in the array / datastore you pull from when rendering your cells in your delegate methods.
By replacing the code when the table view requests all the data & cells for rendering your delegate methods will pull the new data down and return an updated cell.

iPhone:How do i insert few rows in the middle of existing table?

If i want to insert few rows in the middle of the existing table, how do i approach it? Where all that i should be careful when doing such thing? Is there any good sample source available?
Appreciate your helps.
Thank you.
Tableviews don't contain data and they don't actually contain rows themselves. Instead, tableviews are just an illusion created by redisplaying the same tableviewcell objects over and over again with different data each time. The real table, the real rows, are actually in the logical data.
So, to " insert few rows in the middle of the existing table" you actually add the rows into the data that the table displays. For example, if you had a simple array of names that you display in the table, you would insert the new names into the array. Then you would call -[UITableView reload] which will cause the table to ask the datasource for the new data.
See the Table View Programing Guide for iPhone OS.
Insert the data into your model and call reloadData on the table. If you need to animate the insertion for some reason, you can use insertRowsAtIndexPaths:withRowAnimation:, but the principle if updating the model first still applies.