3

I just switched over to using MKNetworkKit and I'm confused whether I should create one MKNetworkEngine object at App launch and use that whenever I want a net connection, or if I should be creating a new one each time?

If I needed to create just one and use it throughout the lifetime of the App, I would have assumed MKNetworkKit would create a singleton, but it doesn't seem to (not that I can see).

On the other hand, it seems cumbersome to alloc/init MKNetworkEngine every time I want to do a simple GET request.

Something seems amiss, so I'm wondering it I'm using it correctly.

Roger Gilbrat
  • 3,755
  • 5
  • 34
  • 58

2 Answers2

1

you create one engine for every webservice you intend to call and keep it as a strong property in the app delegate
(facebook = one engine / twitter = one engine / flickr = one engine)

The creator Mugunth Kuma writes this:

MKNetworkEngine is a pseudo-singleton class that manages the network queue in your application. It’s a pseudo-singleton, in the sense, for simple requests, you should use MKNetworkEngine methods directly. For more powerful customization, you should subclass it. Every MKNetworkEngine subclass has its own Reachability object that notifies it of server reachability notifications. You should consider creating a subclass of MKNetworkEngine for every unique REST server you use. It’s pseudo-singleton in the sense, every single request in any of it’s subclass goes through one and only one single queue.

see this blog article

Etienne678
  • 444
  • 4
  • 12
1

Etienne678 is right. You should create an engine for every webserver and set the global headers (like gzip) and others required by that server on the engine.

When you queue an operation, to an engine these headers are automatically appended.

Mugunth
  • 14,461
  • 15
  • 66
  • 94