Loading Data Asynchronously into UITableView - ios

I am using xCode 7 beta and swift, and storing my data using Parse.com. In which method in the UITableViewController should I be asking for data from my database? I need to ensure it is all present by the point in the lifecycle of the controller at which the cells need to be presented.
Is there anyway to guarantee this if I am loading data asynchronously?

viewDidLoad would be a good place for this but there might be a few second difference between displaying a blank tableView and when it gets populated depending on how long it takes to pull your data.
So in viewDidLoad you'll tell the Parse SDK to pull data and when it's complete, you can call [self.tableView reloadData]

You should be asking for your data before the tableview is shown. That way you can pass your cells the ParseObject within the cellForIndexPath function. Then within your cell you can go ahead and get other information from ParseObject such as fetching relations or PFFIles etc

Related

iOS tableview animated placeholder

Is it possible to use below library in ios when the tableview data is getting from server. Initially the row count will be zero until we get the data from server.
https://github.com/malkouz/ListPlaceholder
https://github.com/samhann/Loader.swift
Please help me if its possible using this.
I need to show FB like placeholder in tableview until I get data from server.
It certainly looks like it does what you want - although I'm not sure how customisable it is? Grab the 'Example' project and have a play!
However, I'd suggest taking a different approach and just return a row count of 1 while your loading your data. Then in your cellForRowAt: method create a 'loading cell' if your still loading which you can style to look exactly how you want. When your data finishes loading just call reloadData on your tableview, this time returning the correct row count and returning your fully populated cells.

TableView rows not updating after core data insert

I'm very new to swift programming. I have been playing around with this for a while, but I am not getting anywhere so asking here.
I have a tableview which I can load the data into the view from CoreData no problem. I have an ADD button at the top that segue's to a new tableview, with a long list of options they can pick from. This tableview also works fine, and includes a search bar.
When the user selects an item row from the second tableview, it inserts that item into CoreData and segue's back to the first tableview. This is where my problem is, the data does NOT update on the visible view.
I call tableview.reloaddata() and I can see my code calling the fetchedResultsController with the new query that would return with the new data. But the code never gets to the cellForRowAtIndexPath func so therefore the visible data view never changes. It remains the same display that was visible when the add button was pressed.
How does the visible data get updated? What am I missing?
When using an NSFetchedResultsController, if you want it to "automatically" update the contents of the tableview then there are a couple of things you need to make sure of...
You have to become the NSFetchedResultsControllerDelegate and implements the methods necessary for the updating of the table view. These are quite lengthy and can be found on the Ray Wenderlich website. The code on here is in Objective-C but it's fairly easy to convert to Swift.
The second thing you need is to make sure that the core data update is done on a background thread. Again the website linked above shows this.
Once you've done that then you don't actually need to run [tableview reloadData] because the fetched results controller methods will manage everything for you.

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).

ASIHTTPRequest async updating uitableview

I'm using a UITableView which loads data from a server. I need to use async ASIHTTPRequest, which is in a separate .m.
How could I force to reload the table data when the requestFinished:request is called?
Thanks!
You can just send the data that needs to be loaded into the TableView by passing it between files. and then finally call a different method of inside the table view that implements the command [self.tableview relaodData];
Update:
What i see from your comments is that the you have a tableView CONtroller or someform of controller that loads the data by calling another class that holds the ASIHTTPrequest/ ansynchrinous requsts. now You have also told me that the TableView Also hold a UIViewController in each cell which i Dont think is a good idea you could go with a custom cell method have a look at this 4 part tutorial... now and the data that needs to be loaded into the TableView is loaded by TableViewController by some method wither it be each cell or something... so after you are able to get the data just inside the tableViewController just Call the [self.tablebview reloadData] inside the TableViewController which is the parent of the TableView there is no need to call in the other methods.
Finally solved with observers although when the app is completely closed won't work. I'll think in something.
Thanks

Populate UITableView with results of webservice

I have a table view which i want to populate with the results (XML) of my call to a web service.
The NSURLConnection and NSMutableURLRequest that are doing the setup for this are currently in my -viewDidLoad method, and i also have all of my UITableView delegate methods in my .m file too.
The data is being returned and added to my array correctly. My problem is (i think) that the UITableView methods are being called before any data has been returned from the web service, which is why my table view is always blank.
How can i call the methods in the right order (if this is even the problem)...
Are you calling reloadData on the tableview once you've repopulated the array? You need to let it know that you have new data.
UITABLEVIEW *mytableview=[[UITABLEVIEW alloc]init];
[mytableview reloadData];
Yes you are right; the UITABLEVIEW delegates are called before the web service. So you have to call those delegates again so try reloading the data. Works perfect for me. reload table when u have something returned from the webservice. store it in NSArray or whatever and then reload table.
Check out Apple's SeismicXML code example on the developer.apple.com/iphone site. It shows how to use NSURL, NSXMLParser, and a TableView to do just this type of thing.

Resources