Firebase and table view duplicating data swift - ios

I use firebase database and table view. I put there an object. The code:
thingManager.addNewThing(thingName.text!)
let thingRef = self.thingsRef.childByAppendingPath(thingName.text!)
thingRef.setValue(thingManager.toAnyObject(thingName.text!)
All of the elements from database are displayed in table view. Table view is loaded everytime the application starts.
I load the table view by having an array from which table view takes info. Everytime the app is loaded, the array is filled with elements from database.
The problem is that the elements duplicates in table view when I add new object to the database. The data doesn't duplicate in the database itself, but it duplicates somewhere in that array. Tried multiple things and when I delete the last two lines
let thingRef = self.thingsRef.childByAppendingPath(thingName.text!)
thingRef.setValue(thingManager.toAnyObject(thingName.text!)
everything works fine, but the data is now not saved in the database. My question is: can these two lines cause the duplication? Thanks in advance

I had this same issue. I was loading the Firebase Data into a GLOBAL Array of structs. I was able to fix my issue by switching the Array to a LOCAL array in my tableView class. Let me know if this helps:)

your code is not very clear, but i think you need
dispatch_async(dispatch_get_main_queue(), {
self.YourTableView.reloadData()
})

Related

Select cells from array UITableView

I have an array of users
var selectedUsers = [User]()
users are added to this array as they are selected in a UITableView (see screenshot: https://www.dropbox.com/s/uk03mhgi3x4jesy/File%2006-10-2015%2C%2018%2003%2044.png?dl=0)
What I'm having difficulty with is when I press back and then reload the view controller, all the checkmarks disappear.
Is there anyway I can keep track of the selected cells?
Thanks in advance
You could use a delegate to pass a array between the two viewcontrollers. For example, each time you select a row, you can store the userId's associated with the row, in the array. So if you were to press back and then open the view with the UITableView, before you load the UITableView, you can first check if there is an array being passed to the viewcontroller containing the UITableView / if there is an array, check if the count is greater than 0. If the array is not empty, then use a for loop to cycle through the array of users being displayed with the array being passed containing all of the previously selected id's, then add the check marks that match up to the Id's in the array.
Here is a tutorial that possesses a similar example: http://makeapppie.com/2014/08/04/the-swift-swift-tutorial-why-do-we-need-delegates/
The answer to this question is kinda dependent on what you want the application to do... Do you want the User selections to persist even if the app is closed? If so consider using NSUserDefaults or CoreData. Otherwise store the data somewhere that does not get blown away (maybe the root view controller of your application). You could for instance implement the delegate pattern on the view controller with the table view and pass the values to the container view controller.. Just some ideas.
So you can use class NSUserDefaults to save small amount of data. If you want to have persistent store so you have to use Core Data that helps you store you data with scheme you provide.
If you just want to pass data from one view controller to another view controller so you just need use delegation pattern or you can use method prepareForSegue where you can pass some data as well but you can't store this way, just pass.
If you make your question more detailed you will get more explicit answer.

Loading new cells added from Parse data class

I'm creating an app for clothing shop and I'm using parse cloud to upload and retrieve data from it. But when I add a new item to the data class, it loads at the bottom of the tableview.
How can I make the new items or the new cells load from the top of the table view like at the first cell?
I'm using PFQueryTableView from Parse instead of the original tableView.
If you are using PFQuery then you can trial & error using:
– orderByAscending:
OR
– orderByDescending: - right after you update the data source.
Also, you might want to check out This Discussion.
Let me know, if it works for you.

Model NSArray mutable or immutable?

I never realised this dichotomy before so I need to ask you.
Let's say we have some kind of model manager class that will expose an NSArray of objects via public property. This is our model.
Now I also have a view controller that shows members of this array in tableview cells.
I set this NSArray as a datasource for tableview. But what if I want my model change data in time?
I have two options.
1) Make the array mutable.
2) Replace the instance NSArray with a different instance containing new data.
The issue with option 1 is anybody can change array's content which seems wrong.
The issue with option 2 is the tableViewController will happily keep pointing to the original array instance and ignore that the manager class is now pointing to a new instance (since it replaced it's property array instance with one having updated data.).
To sum it up, I want an array instance that can only be mutated from the model manager, but would be immutable to outside world. Which is impossible right?
Any ideas how to solve this problem?
Either the object managing the array should also be the table view's data source or the table view's data source should always make sure to get a fresh copy of the array from the object managing the array just before the table view reloads its data.
Either way, the array that the table view's datasource is working with should, in the end, be an immutable array, and any time this array is changed, a call to reloadData should immediately be made.
This will prevent the data in the array being modified in the middle of the table view displaying data. It's very problematic if the contents of the array change after numberOfRowsInSection: has been called, for example.

Consolidate Table data into one

I have 4 different views, all that have their own table view with data. I want to make a 5th view/interface that has a table view that consolidates the other 4 tableview data into one.
For each of the 4 initial table views, I am storing the data using Core Data, and each table has its own entity with multiple attributes. Im looking for the theory about how to consolidate all these into one tableview. Any help would be great, or if you can point me in the right direction.
So essentially i have 4 table views all with their own data, and i want to make a 5th table view that shows all the data.
Thanks so much!
I guess what I would do is to store all the data of all the table views in a single mutable dictionary that contains sub dictionaries that contains each table view data like so:
MainDictionary >
---tableview1Dictionary
---tableview2Dictionary
---tableview3Dictionary
---tableview4Dictionary
I would save this dictionary on the NSUserDefaults so it can be accessed from each view controller in your app.
On the fifth view controller you can simply load this MainDictionary data in to your "combined" table view. This way whatever changes you make on any table view will directly effect your combined table view and all your data will be synced.
You can reload each table view on your controller viewDidAppear.
NSUserDefaults Tutorial: http://www.icodeblog.com/2008/10/03/iphone-programming-tutorial-savingretrieving-data-using-nsuserdefaults/
Hope that helps.

Moving cells from one table to another in iOS

I am trying to create an application with multiple table views that uses and implements Core Data. I would like the user to be able to select cells in one table and move them to another (like in Apple's mail application) using either a check accessory or a selectedCell method with an action sheet. I'm stuck because I don't know if you are actually moving the cell to another table or if you are adding a copy to the new table and deleting the original. Basically, I'm asking for a basic example of cell movement to give me a push in the right direction.
You won't be moving cells. The model for a table view is an array. Move things between the arrays and tell the tables that their model has changed.
id somePartOfMyModel = [self.arrayA objectAtIndex:someIndexPath.row];
[self.arrayA removeObject:somePartOfMyModel];
[self.arrayB addObject:somePartOfMyModel];
// the simplest, non-animated way to update the tables.
// I'd advise getting this working first, then later trying fancier UI to indicate changes
[self.tableViewA reloadData];
[self.tableViewB reloadData];
You would not be technically moving the cell to the other table. The way I would go about doing this would be to pass the NSManagedObjectContextID of the item between the tables, depending on how large your entities are and if the tables are in the same view controller.

Resources