Wednesday, November 18, 2015

NSURLSession delegate and data task delegate for CustomHttpProtocol sample

Apple provides a sample project CustomHttpProtocol to demonstrate how NSURLProtocol works with NSURLSession.

Note the project includes a file QNSURLSessionDemux, which involves how NSURLSession' delegate works.

When creating a NSURLSession object from NSURLSessionConfiguration,
+ sessionWithConfiguration:delegate:delegateQueue:
a delegate of NSURLSessionDelegate can be specified in the delegate parameter. Apparently this delegate will be used to handle NSURLSessionDelegate event.

When creating the data task from the NSURLSession object, Apple defiends NSURLSessionDataDelegate and NSURLSessionTaskDelegate to handle the per task event. However, unlike NSURLSession, when creating the data task, there is no parameter in
- dataTaskWithRequest:completionHandler:
method to specify a delegate to handle the per task event. Instead the delegate object passed for creating NSURLSession will also be used to handle the per task delegate event.

Then the problem comes, as the delegate for NSURLSession is a shared delegate object for all data tasks, but each task has its own logic to handle the response data as well as mode and thread, so once the NSURLSession's common delegate get the per task delegate event, it has to find a way to route back to the per task delegate to handle it.

QNSURLSessionDemux in CustomHttpProtocol is created for this purpose, it keeps a dictionary of per task data delegate specified by application when the data task is created. Then the NSURLSessionDelegate will route the per task event their corresponding per task delegate.

Actually, NSURLSessionTaskDelegate and NSURLSessionDataDelegate are all derived from NSURLSessionDelegate, which implies the NSURLSession delegate will also be used to handle per task event.

One last note, when creating the data task, if custom delegate will be used to handle NSURLSession's data task event, then use the method that does not take the completionHandler. Otherwise, if completionHandler exists, then the default system delegate will be used, and custom delegate will not be invoked.  

SAP UI5: Logic to start a tile from launchpad

Shell-dbg.controller.js openApp() method is called, which then calls NavContainer-dbg.js to method.
Render manger will call ApplicationContainer-dbg.js createUI5Component method

The UI5 application's component.js createContent method is called, this is the first chance the application's code is executed. The method will create the view.
When creating the view, the application's Main.Controller.js' onInit method is called.

Application-dbg.js main() method is called, which calls ApplicationImplementation-dbg.js startApplication method.

ConnectionManger-dbg.js getNewInstance method is called, which will call initModels() for creating oDataModel based on url and application config (line 171)

oDataModel-dbg.js oDataModel constructor is called to create the model, which will get metadata from server.

The UI5 application view's onInit method is called, which will all getServiceSchemeVersion()

Monday, November 2, 2015

Config proxy for Android emulator for internet access

When running android emulator on a Windows box, if the Windows box is behind a proxy, then the android emulator will also need to config the proxy settings in order to get internet access. This can be easily verified by using mobile chrome browser to access www.google.com, if it fail, then likely it is due to proxy settings. 

The below steps can be used for this:
1. From Android emulator, open settings app
2. Under Wireless & Networks section, click More...
3. click Mobile network item
4. click Access Point Names item
5. click T-Mobile US item
6. Set proxy to your proxy server's name, for example: proxy.phl.sap.corp
7. Set port to your proxy server's port, for example: 8080
8. Go back and open mobile browser, and visit http://www.google.com and it should work