0

I am having a basic question for like best practise.

The setup:

ListViewController: UiTableView with ManagedObjects. The objects will be loaded from a server. First load 20 objects. Scroll to the end of the table, the next 20 objects will be loaded. Selecting a cell will load the DetailViewController. I have a ListObject with an array of the items (and other properties with informations about the list, not relevant for the DetailVC)

DetailViewController: The details of the selected object are shown. The VC also has 2 buttons to show the next object details or the previous object.

Now e.g. there are 20 objects loaded in the ListVC and I select row 10, the DetailVC with Object at index 10 will show. Then I click trough the next objects until object number 20. Now when i click the 'next' Button, the next 20 objects have to be loaded from the server. (Show object #21 but there are only 20 objects loaded).

Is there a best practice to load the next objects? I have a class for data loading. - (void)loadDataWithRequest:completionHandler: When the NSUrlSession DownloadTask finishes, the completionHandler gets called and this calls the class ApiParser for json parsing and adds the result to the List Object.

So i have to call this in the DetailVC for the next 20 objects.

Now what is the best practice to do this? Or is there a better way to implement the data loading?

I could pass a ListVC reference to the DetailVC and call [listvc loadDataWithRequest:completionhandler] and load the detail from [listvc.listobject.items objectAtIndex]

Or i just could pass the ListObjectItems to the detailVC. Somehow load the new objects and have a KVO to the ListObjectItems count.

Other methods would be delegates or NotificationCenter.

But i guess the best practice is not to put the loadData Method in the ListVC, but somewhere else.

How about to put the dataLoad method as instance method in the ListObject class and listen for the KVO in ListVC and DetailVC?

So many possibilities. But what is a good way?

marco
  • 63
  • 1
  • 7
  • You have two things that are both responsible for "displaying a detail view". Try to make that just one thing. – CrimsonChris Jul 23 '14 at 19:57
  • Moving the loading code out of the view controller would definitely be a good start. – CrimsonChris Jul 23 '14 at 19:57
  • Also, consider this scenario. What if you are on item #60 and you hit the "next" button. What happens if #61 doesn't exist? Meaning that there are only 60 items total to get from the server. – CrimsonChris Jul 23 '14 at 20:00

3 Answers3

1

When the "next" or "previous" buttons are pressed you could inform the "master table view" of the action. This would then load the next batch if necessary before presenting the detail view. Don't block the UI though, show a spinner or something in the blank detail view while waiting for the next batch to come back.

In summary: don't put any loading logic in your detail view. It should only be responsible for displaying the "details" and possibly a loading spinner if you tell it to display nil (as in, not finished loading yet).

CrimsonChris
  • 4,651
  • 2
  • 19
  • 30
1

Ideally, you would handle the data to be diplayed, in this case the ListObject, seperate from the view controller which is displaying the data, in this case the ListVC or DetailVC.

Each VC should then keep a reference to the data source, which could be a class holding the ListObject, called ListDataSource. This class should hold the methods for loading data from the server. Then, each VC can tell the ListDataSource to load 20 more objects.

This delegation of responsibilities is a pretty good example of a common programming paradigm called MVC. More on that here.

Community
  • 1
  • 1
ufmike
  • 93
  • 8
0

Sorry for the late response and thanks for all the replies.

I now have a ListObject method to load the next data. And in the ListVC and the DetailVC I use KVO to listen to changes to the ListObject.

Works good so far...

marco
  • 63
  • 1
  • 7