Query into Table View Controller - ios

I use Parse for my backend data. Here, I saved an array of dictionaries to then query into my app and load into my tableview.
Here is my array saved in Parse:
When I query it in the viewDidLoad I set my empty array (self.dealListArray) equal to the array on Parse.
Here is what it looks like:
The first NSLog() outputs the entire array from Parse perfectly. But the second NSLog outside of the brackets output NULL.
The table view will not load up because my array is NULL. How can I keep the information from the query so that I can fill it with my table view?
Thank you, any help is much appreciated

You have no real guarantee when the block is called. So it's probably best that you wait until the data is loaded and meanwhile to have a UIActivityIndicatorView or something similar to indicate that something is being loaded.
Also, when the block does get called and you change the value of dealListArray you would have to reload the tableview's data.
like this...
self.dealListArray = [resMenu objectForKey#"Deals"];
NSLog(#"%#", self.dealListArray];
[self.tableView reloadData];

Related

Firebase and table view duplicating data swift

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()
})

Objective C MutableArray delete empty row

I have a problem with my tableview. When I tap the add button in my tableview it sends me to a textview where I can write a text. Then when I save it, it moves back to the tableview and create a new row with the text from the textview as title. If the textview is empty it saves it with no text, and create a row without a title in the table view.
I want to delete all rows without a title in the tableview. I save the text to a NSMutableArray named "savedText".
Here is some code I have tried:
if ([savedText containsObject:#""]) {
savedText=nil;
}
and this:
if ([savedText isEqualToArray:#""]) {
savedText=nil;
}
Nothing happens when I use this code.
Conny, one of us doesn't understand or is missing something here. It's probably me, but just to try to help out anyway.
As I understand it you're storing all the entered strings into the array named savedText and want to go through all the items in the array to check if they're empty and remove them from the array if they are. Is that correct?
GuybrushTheepwood is right in his comment, you could simply check before storing it in the array. If it's empty, just don't store it and you'll never have to make the processor work to remove it.
However, if you still want an algorithm for removing empty strings you'll need to try something else. Your first if block will get rid of the entire array if it contains something that's empty, and your second one should be giving a warning that the parameter isn't an NSArray*. Maybe your looking for something like this?
for (NSString* string in savedText) {
if ([string isEqualToString:#""]) {
[savedText removeObject:string];
}
}
// Potentially more code here? Like reloading the tableview?
But that'll only remove it from the array, you'll need more than that to update the view.

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.

App getting crash while reloading the data

My tableview is getting reloaded and gets new data for every 10secs by running timer in background thread.But when data is getting reloaded and the same time when I'm scrolling the tableview .
App is getting crashed.leaving the error as
[__NSArrayM objectAtIndex:]: index 18 beyond bounds [0 .. 15].
You need to remove objects from your array when you get the response. I guess you are removing your array first and then your timer is executing.
You haven't provided any code, so this is a it of a guess, but as others have said it seems likely that you are reloading your data directly into the array that is your table data source. This is causing a timing related issue where your data source array and the information you have provided to the table view previously (specifically number of rows) is inconsistent.
You should fetch your new data into a different array and then once all data has been fetched you can update the reference to your table data source array and reload the table view.
Another approach is to compare the two arrays to determine which rows have been deleted and which rows are new. You can then call delete/insert rows as appropriate. Make sure you call beginUpdates before you start adding/deleting rows and endUpdates once you have finished.
Please try to reload your table in main thread. Like below code
dispatch_async(dispatch_get_main_queue(), ^{
[self.collection reloadData];
});

How to populate an UITableView when JSON response arrives?

I am looking for a sample code, showing what is the best way to populate an UITableView with the response of JSON data. Let me explain in details.
The situation is the following:
When I navigate to the current view controller(the one with the table view), I send a request to my server in order to send me a JSON response. Meanwhile, I display an Activity indicator in foreground, showing the user that an activity is loading. When the response arrives, I need to parse it, store it with Core Data and then populate my Table View with the data.
My question is:
What is the best practice, best way to populate the table view? Should I grab the whole data that I need and use a NSArray while populating the table view? Or may be use NSFetchedResultsController for this purpose? How should I reload the table view? Is [self.tableView reloadData] good enough for my purpose?
I hope I was clear enough with my question(s) :) Thanks in advance
If the view controller always needs to call out to the server on every view, you can simply retain the response dictionary (or convert it to your own plain objects and retain an array of those) and bind the tableview in the afnetworking callback.
If you do not always need to call it, you could save the objects to core data on call back then create your retained NSFetchedResultsController and set the delegate on it (just in case the data gets modified/altered elsewhere).

Resources