1

I'm working now on my first really big app to a client. This app will have a server communication and data download/upload.

I want to create a good server communication design at the beginning so i wouldn't have to change it a lot when it hits some problems.

My question is about NSURLSession. As i see it i have 3 options:

1 - Create a serverCommunicator helper class that will managed all of the server traffic

2 - Do all the communication with the server from the viewController itself

3 - Create a helper class only to create the session task, and in the viewController use that task with a completion handler to manage the returned data

What would be the better Design? if there is another option or a helper class you know please let e know..

Thanks,

Wain
  • 118,658
  • 15
  • 128
  • 151
YYfim
  • 1,402
  • 1
  • 9
  • 24
  • What about a data controller to encapsulate local / remote collection away from VCs? What about using AFNetworking / RestKit? many things to consider, you haven't given details of what services you will use, what they respond with, local data storage, etc... – Wain Apr 10 '14 at 11:30
  • Hey Wain, i'm using NSURLSession to create the requests, i have a CoreData for local db, and also the server db. – YYfim Apr 10 '14 at 11:33
  • What features of `NSURLSession` do you require? Mapping returned JSON direct into Core Data? – Wain Apr 10 '14 at 11:35
  • Sometimes i will need to persist to CoreData or to the NSUserDefaults, and sometimes just present the data back to the user – YYfim Apr 10 '14 at 11:40

1 Answers1

1

It's a bit opinion based, but:

I'd use a data controller class. My view controllers would know nothing about the difference between local / remote data. Everything about data access would be controlled by the data controller and the interface it provides would always be asynchronous capable (using callback blocks).

I'd drop NSURLSession and use RestKit (which doesn't support NSURLSession yes, support is work-in-progress) because it makes mapping the external data model into your internal data model very easy. If I did need to use NSURLSession for something, fine, but I'd make that usage explicit only for that activity.

My data controller would own the Core Data stack (though RestKit would create the stack) and mediate access to it. The data controller would also be a singleton. The data controller is the only class that knows about the external data model (though some of the internal data model classes may contain validation logic used during the conversion from external -> internal).

Wain
  • 118,658
  • 15
  • 128
  • 151
  • So you recommand not to use a helper class and using an instance of it to each operation? And what do you mean by dataCntroller? – YYfim Apr 10 '14 at 12:34
  • Personally, yes, I would not use a helper class because that gives knowledge of networking requirements to the VC. Personally, I like to separate that knowledge. – Wain Apr 10 '14 at 12:40
  • Decided tot go with your advice and transition to RestKit thanks – YYfim Apr 14 '14 at 05:34