How to keep Realm data in the same order as entered - swift

I have an application where I need to keep the data in the same order as it is entered. The data is entered into a List property. Everything works great until I have to delete an item. When the delete occurs the last item on the List takes the place of the one that was deleted. The UITableView shows the correct number of items but is not synchronized with the realm List.
An example would be if I had data:
1, 2, 3, 4, 5, 6
I then delete:
3
The realm data looks like this:
1, 2, 6, 4, 5
The tableview looks like this:
1, 2, 4, 5, 6
The desired result of the realm data would match the tableview output.
Is there any way to keep the order of the data from changing after items are deleted?

Objects in a Realm are inherently unordered. If you wish to retrieve objects in a specific order that you control, you can do so by inserting them into a List property of an object, or by sorting the Results that you use to retrieve them.

Related

Getting the underlying data in tableau

The data representation was created in Tableau and I am accessing and showing it on my website through the Tableau JS API: link to the API.
Obtaining the viz and showing my Tableau have already been done. I have a Bar Chart showing my data in horizontal columns. The idea is that I want to be able to click on a column and then get the data which goes into this column.
Let's say we have 12 elements. They are represented by 3 columns. The first column has 5, the second 3 and the third 4 elements. In my case, after selecting the column with 4 elements I would want to be able to get the underlying 4 data entries.
I have been going through the API Documentation (link) and I have added on click listener for marksSelection. However, it only returns me the "sorting conditions" so to say. I tried getting the getUnderlyingDataAsync and then getData() but the returned data is not really in a format which I can use (since there are only pairs of row and column and not the full data entries).
Is something like this possible?
I solved it in a couple of steps following the API:
getUnderlyingDataAsync to obtain a DataTable, save it to an object called dataTable.
Get all of the (raw) data by calling dataTable.getData(). It returns a 2D array. To help you imagine the structure, let's say that the data source has 50 objects then the array will have a length of 50. If each object has 5 properties then the nested array will have a length of 5 (with each property represented as 1 element).
Get all of the columns by calling dataTable.getColumns(). Here you will need the name and index properties.
Create an object containing our actual JSON data by combining the columns in (3) and the (raw) data from (2).
Now you should have a structured array of objects.

Wrong Item Id in Collection

I have collection field type in my form. And I have problem with Id of a field. When page is loaded for example I've got three items in my collection. In this case they should have ID's 0, 1 and 2 but They've got 4, 5, 6....
<input type="text" id="form_crm_contact_person_contactPhone_4_value" name="form_crm_contact_person[contactPhone][4][value]">
Later when I add new item It has Id 4, and it would be okey when previous items would have correct Id's. But in this case new item is updating this old item instead of being a new one - when the form is saved...
Any ideas what can be wrong ? I have to say that in other collection in my application everything it's okey... Rest start with zero.

Save Entities in a Specific Order

I am using Entitiy Framework and have come across a weird problem.
I am trying to save a collection to the database (say: Collection of Rounds).
Now each item in this collection in turn has a collection of child elements (say: Collection of Events).
Which would look something like this:
Round 1
(No Child Elements)
Round 2
Event 1
Event 2
Event 3
Round 3
(No Child Elements)
Round 4
Event 1
Event 2
As shown above it is not necessary that the parent object will always have a child collection.
Now here is the problem:
My requirement is that i want to save the data as i have added it to the collection.
But, while saving EF saves the items having a child collection first and so the order is modified upon saving.
So, in the database Round 2 is saved first and then others are saved randomly.
Is there any way to force EF to save the Rounds collection in the order that i have constructed it??
It should always save starting from Round 1 and should end with saving Round 4.
Thanks guys : )
Neither database or EF guarantees ordering. In the same way if you query the database you don't have to get elements in expected order. If you want exact order you must add additional column to keep record's ordering value and use OrderBy extension method when retrieving data.
Order of operations executed by SaveChanges is in full control of EF. You cannot change it.

Preventing duplicate NSArrays (one sorted, one unsorted)

I have an NSArray of objects, it is unordered. It's updated from the internet so I can't order it once and guarantee it's order later.
Each item corresponds to a table view cell, but the user can reorder these, and there are 2 sections in the tableview, although to begin with all the cells are in one section.
So I could create a duplicate NSArray and have this ordered. And save it to the hard drive.
But this seems quite a waste, and what do I do when a new object is added to/removed from the unordered NSArray by an update over the internet.
So in sum:
How do i have one unordered NSArray and one ordered without duplication (and waste of memory)?
And how do I deal with updates to the unordered array, when the user hasn't set a location for new objects/cells?
Your question is not very clear:
Arrays are an "ordered collection" by definition (apart from the odd languages with "associative arrays").
"Ordering" is not the same as "sorting". [1, 2, 3] is sorted; [1, 3, 2] is not sorted, but it is ordered (assuming the usual comparator).
"Preventing duplicate NSArrays" is an unhelpful subject.
So I'll have to guess at what you're trying to say:
You have a list of things that you download from the internet.
You occasionally update the list of things downloaded.
The user can reorder the list.
You want to be able to update the list of things but preserve the user's ordering.
Well, first you need some way of finding out which items in the two lists are "equivalent". For example, the first list is [Apple, Banana, Orange] and the user puts it in juice-preference order [Orange, Apple, Banana]. If the second list is [apple, banana, orange] (because you decided that everything should be lowercase, and things that needed to be capitalized could be done with -[NSString capitalizedString], or whatever), you need a way of determining that Apple = apple and constructing a new list [orange, apple, banana].
It's not clear why you think you have to save the original list — yes, it means that you can just say "Apple and apple are both at index 0, so they're the same", but this also means that you can never change the default order, and you can never remove an item (you can replace it with a placeholder, but meh).
There are two easy solutions:
Keep a list of indices (e.g. you'd store [2, 0, 1] because Orange is at index 2, etc.). This is a bit of a pain. If you want to use NSArray, the easiest way is to wrap things in NSNumber.
Don't care about the original ordering. Let's say you have a "user-ordered" list [Orange, Apple, Banana] and a new "server-ordered" list [apple, grape, orange]. Iterate over the user-ordered list, picking out "equivalent" items from the server-ordered list to get [orange, apple] and [grape]. Then do something sane with the "new" items, like sticking it on the end to get [orange, apple, grape]. (In this example, Banana has been removed; we handle this case by simply not including it in the new list.)
If they're both referencing the same objects (and not copies), then the extra memory will be very minimal (NSArray overhead and pointers), and probably not worth worrying about.

Batch Insertion & Deletion + Reloading old cells = Exception

So I have a UITableView in which I am doing batch insert/delete/reloads into. Every once in a while, the list data changes, so I batch update the differences in the list. I'm basically inserting new rows, deleting rows that are no longer there, and reloading rows that exist in both the old and new data. For example:
Before, the list data looks like this:
0: apple
1: banana
2: carrot
After, the list data looks like this:
0: banana
1: carrot
2: dog
3: elephant
This results in deleting row 0, reloading rows 1 and 2, and inserting at rows 2 and 3. The order I call the methods is also in this way: delete, then reload, then insert rows.
However, this results in a exception being thrown since I'm doing two different animations on row 2 (reloading and inserting). Is this an ordering issue, or are my indexPaths incorrect? Note: I need to reload old cells, since the data for that row may have changed, but should not be represented by an insertion/deletion.
Edit: Weirdly enough, this error only happens in iOS versions earlier than iOS4.
I'd say delete 0, and insert 3 and 4. Reloading shouldn't be needed. The order in which you send the updates is irrelevant. Make sure that your model reflects the changes before you tell about the updates. Wrap the calls up with -beginUpdates: end -endUpdates:.
Here is a link for batch update, see listing 7-8:
https://developer.apple.com/library/content/documentation/UserExperience/Conceptual/TableView_iPhone/ManageInsertDeleteRow/ManageInsertDeleteRow.html#//apple_ref/doc/uid/TP40007451-CH10-SW16
The WWDC video hint is good, too. You want to look out for Session 128 - Mastering Table Views.