Titanium JIRA Archive
Titanium SDK/CLI (TIMOB)

[TIMOB-23116] iOS: Add sessionIdentifier to URLSession events

GitHub Issuen/a
TypeImprovement
PriorityMedium
StatusClosed
ResolutionFixed
Resolution Date2016-03-28T17:11:28.000+0000
Affected Version/sn/a
Fix Version/sRelease 5.4.0
ComponentsiOS
Labelsios, nsurlsession, qe-5.4.0, sessionIdentifier
ReporterHans Knöchel
AssigneeHans Knöchel
Created2016-03-27T16:02:58.000+0000
Updated2016-08-05T17:52:54.000+0000

Description

There is a community PR to add the sessionIdentifier to the URLSession: https://github.com/appcelerator/titanium_mobile/pull/6941 We need to do the following: * Nil-validations for the case that the sessionIdentifier is nil * The docs for the new property, as well as the docs for the uploadprogress event * The missing dictionary in URLSessionDidFinishEventsForBackgroundURLSession.

Comments

  1. Hans Knöchel 2016-03-27

    PR: https://github.com/appcelerator/titanium_mobile/pull/7889
  2. Hans Knöchel 2016-08-04

    Test-case: 1. Follow [this guide](http://docs.appcelerator.com/platform/latest/#!/guide/iOS_Background_Services-section-37539664_iOSBackgroundServices-URLSessionModule) to use the urlSession module with Titanium 2. Inside the downloadprogress and downloadcompleted event, watch out for the sessionIdentifier property when it is called.
  3. Josh Longton 2016-08-04

    [~hansknoechel] I am getting an invalid session error when testing the sessionIdentifier.

    Error

       [ERROR] :  Error: invalid session
           at makeError (/Users/Josh/.appcelerator/install/5.4.0-38/package/node_modules/appc-platform-sdk/lib/auth.js:87:14)
           at /Users/Josh/.appcelerator/install/5.4.0-38/package/node_modules/appc-platform-sdk/lib/auth.js:256:21
           at Request._callback (/Users/Josh/.appcelerator/install/5.4.0-38/package/node_modules/appc-platform-sdk/lib/index.js:354:11)
           at Request.self.callback (/Users/Josh/.appcelerator/install/5.4.0-38/package/node_modules/request/request.js:187:22)
           at emitTwo (events.js:87:13)
           at Request.emit (events.js:172:7)
           at Request.<anonymous> (/Users/Josh/.appcelerator/install/5.4.0-38/package/node_modules/request/request.js:1044:10)
           at emitOne (events.js:77:13)
           at Request.emit (events.js:169:7)
           at IncomingMessage.<anonymous> (/Users/Josh/.appcelerator/install/5.4.0-38/package/node_modules/request/request.js:965:12)
           at emitNone (events.js:72:20)
           at IncomingMessage.emit (events.js:166:7)
           at endReadableNT (_stream_readable.js:913:12)
       ---------------------------------------------
           at Request.init (/Users/Josh/.appcelerator/install/5.4.0-38/package/node_modules/request/request.js:190:10)
           at new Request (/Users/Josh/.appcelerator/install/5.4.0-38/package/node_modules/request/request.js:129:8)
           at request (/Users/Josh/.appcelerator/install/5.4.0-38/package/node_modules/request/index.js:55:10)
           at Function.get (/Users/Josh/.appcelerator/install/5.4.0-38/package/node_modules/request/index.js:63:12)
           at _createRequest (/Users/Josh/.appcelerator/install/5.4.0-38/package/node_modules/appc-platform-sdk/lib/index.js:429:39)
           at Function.AppC.createRequest (/Users/Josh/.appcelerator/install/5.4.0-38/package/node_modules/appc-platform-sdk/lib/index.js:387:9)
           at /Users/Josh/.appcelerator/install/5.4.0-38/package/node_modules/appc-platform-sdk/lib/auth.js:251:8
           at /Users/Josh/.appcelerator/install/5.4.0-38/package/node_modules/appc-platform-sdk/node_modules/async/lib/async.js:251:17
           at /Users/Josh/.appcelerator/install/5.4.0-38/package/node_modules/appc-platform-sdk/node_modules/async/lib/async.js:154:25
           at /Users/Josh/.appcelerator/install/5.4.0-38/package/node_modules/appc-platform-sdk/node_modules/async/lib/async.js:248:21
           at /Users/Josh/.appcelerator/install/5.4.0-38/package/node_modules/appc-platform-sdk/node_modules/async/lib/async.js:612:34
           at /Users/Josh/.appcelerator/install/5.4.0-38/package/node_modules/appc-platform-sdk/lib/auth.js:110:3
           at next (/Users/Josh/.appcelerator/install/5.4.0-38/package/node_modules/tough-cookie/lib/cookie.js:994:9)
           at MemoryCookieStore.putCookie (/Users/Josh/.appcelerator/install/5.4.0-38/package/node_modules/tough-cookie/lib/memstore.js:118:3)
           at withCookie (/Users/Josh/.appcelerator/install/5.4.0-38/package/node_modules/tough-cookie/lib/cookie.js:1013:13)
       ---------------------------------------------
           at cli.addHook.post (/Users/Josh/Library/Application Support/Titanium/mobilesdk/osx/5.4.0.v20160802165655/iphone/cli/hooks/run.js:52:7)
           at /Users/Josh/.appcelerator/install/5.4.0-38/package/node_modules/titanium/lib/hook.js:235:13
           at /Users/Josh/.appcelerator/install/5.4.0-38/package/node_modules/titanium/node_modules/async/lib/async.js:689:13
           at iterate (/Users/Josh/.appcelerator/install/5.4.0-38/package/node_modules/titanium/node_modules/async/lib/async.js:265:13)
           at nextTickCallbackWith0Args (node.js:420:9)
           at process._tickCallback (node.js:349:13)
       [ERROR] :  Error: invalid session
           at makeError (/Users/Josh/.appcelerator/install/5.4.0-38/package/node_modules/appc-platform-sdk/lib/auth.js:87:14)
           at /Users/Josh/.appcelerator/install/5.4.0-38/package/node_modules/appc-platform-sdk/lib/auth.js:256:21
           at Request._callback (/Users/Josh/.appcelerator/install/5.4.0-38/package/node_modules/appc-platform-sdk/lib/index.js:354:11)
           at Request.self.callback (/Users/Josh/.appcelerator/install/5.4.0-38/package/node_modules/request/request.js:187:22)
           at emitTwo (events.js:87:13)
           at Request.emit (events.js:172:7)
           at Request.<anonymous> (/Users/Josh/.appcelerator/install/5.4.0-38/package/node_modules/request/request.js:1044:10)
           at emitOne (events.js:77:13)
           at Request.emit (events.js:169:7)
           at IncomingMessage.<anonymous> (/Users/Josh/.appcelerator/install/5.4.0-38/package/node_modules/request/request.js:965:12)
           at emitNone (events.js:72:20)
           at IncomingMessage.emit (events.js:166:7)
           at endReadableNT (_stream_readable.js:913:12)
       ---------------------------------------------
           at Request.init (/Users/Josh/.appcelerator/install/5.4.0-38/package/node_modules/request/request.js:190:10)
           at new Request (/Users/Josh/.appcelerator/install/5.4.0-38/package/node_modules/request/request.js:129:8)
           at request (/Users/Josh/.appcelerator/install/5.4.0-38/package/node_modules/request/index.js:55:10)
           at Function.get (/Users/Josh/.appcelerator/install/5.4.0-38/package/node_modules/request/index.js:63:12)
           at _createRequest (/Users/Josh/.appcelerator/install/5.4.0-38/package/node_modules/appc-platform-sdk/lib/index.js:429:39)
           at Function.AppC.createRequest (/Users/Josh/.appcelerator/install/5.4.0-38/package/node_modules/appc-platform-sdk/lib/index.js:387:9)
           at /Users/Josh/.appcelerator/install/5.4.0-38/package/node_modules/appc-platform-sdk/lib/auth.js:251:8
           at /Users/Josh/.appcelerator/install/5.4.0-38/package/node_modules/appc-platform-sdk/node_modules/async/lib/async.js:251:17
           at /Users/Josh/.appcelerator/install/5.4.0-38/package/node_modules/appc-platform-sdk/node_modules/async/lib/async.js:154:25
           at /Users/Josh/.appcelerator/install/5.4.0-38/package/node_modules/appc-platform-sdk/node_modules/async/lib/async.js:248:21
           at /Users/Josh/.appcelerator/install/5.4.0-38/package/node_modules/appc-platform-sdk/node_modules/async/lib/async.js:612:34
           at /Users/Josh/.appcelerator/install/5.4.0-38/package/node_modules/appc-platform-sdk/lib/auth.js:110:3
           at next (/Users/Josh/.appcelerator/install/5.4.0-38/package/node_modules/tough-cookie/lib/cookie.js:994:9)
           at MemoryCookieStore.putCookie (/Users/Josh/.appcelerator/install/5.4.0-38/package/node_modules/tough-cookie/lib/memstore.js:118:3)
           at withCookie (/Users/Josh/.appcelerator/install/5.4.0-38/package/node_modules/tough-cookie/lib/cookie.js:1013:13)
       ---------------------------------------------
           at cli.addHook.post (/Users/Josh/Library/Application Support/Titanium/mobilesdk/osx/5.4.0.v20160802165655/iphone/cli/hooks/run.js:52:7)
           at /Users/Josh/.appcelerator/install/5.4.0-38/package/node_modules/titanium/lib/hook.js:235:13
           at /Users/Josh/.appcelerator/install/5.4.0-38/package/node_modules/titanium/node_modules/async/lib/async.js:689:13
           at iterate (/Users/Josh/.appcelerator/install/5.4.0-38/package/node_modules/titanium/node_modules/async/lib/async.js:265:13)
           at nextTickCallbackWith0Args (node.js:420:9)
           at process._tickCallback (node.js:349:13)
       

    ENV

       iPhone 6s (9.3.1)
       iOS Simulator (9.3)
       
Mac OSX El Capitan 10.11.6
       Studio: 4.7.0.201607250649
       
Ti SDK: 5.4.0.v20160804021257
       
Appc NPM: 4.2.7
       Appc CLI: 5.4.0-38
       Node: v4.4.4
       

    App.js used

       // Require in the urlSession module
       var urlSession = require("com.appcelerator.urlSession");
       var session;
       // App UI
       var win = Ti.UI.createWindow({backgroundColor:"white"});
       var progress = Ti.UI.createProgressBar({
           width: 200,
           height: 50,
           min: 0,
           max: 1,
           value: 0,
           style: Ti.UI.iOS.ProgressBarStyle.PLAIN,
           top: 10,
           message: 'Downloading image URL',
           font: { fontSize: 12, fontWeight: 'bold'},
           color: '#888'
       });
       win.add(progress);
       var imageView = Ti.UI.createImageView({
               top:150,
                 height:300,
                 width:200
           });
       win.add(imageView);
       var button = Ti.UI.createButton({
           title:'Download Image (url)',
           height:40,
           width:200,
           top:70
       });
       button.addEventListener('click', function()
       {
           // Create a session configuration
           // The string parameter is an arbitrary string used to identify the session in events
           var sessionConfig = urlSession.createURLSessionBackgroundConfiguration("com.appcelerator.test");
           // Create a session
           session = urlSession.createURLSession(sessionConfig);
           // Create a background download task to get the asset with the URL
           urlSession.backgroundDownloadTaskWithURL(session,"https://raw.github.com/appcelerator-developer-relations/KitchenSink/master/Resources/images/dog@2x~iphone.jpg");
           progress.show();
       });
       win.add(button);
       win.open();
        
       // Monitor this event to receive updates on the progress of the download
       Ti.App.iOS.addEventListener("downloadprogress", function(e) {
           // Update the progress indicator
           progress.value = (e.totalBytesWritten/e.totalBytesExpectedToWrite);
           Ti.API.info("download progress " + JSON.stringify(e));
       });
       // Monitor this event to know when the download completes
       Ti.App.iOS.addEventListener("downloadcompleted", function(e) {
           Ti.API.info("download completed " + JSON.stringify(e));
        
           // Update the image
           imageView.image = e.data;
        
           // Invalidate the session and cancel current session tasks
           urlSession.invalidateAndCancel(session);
           // Notify the user the download is complete if the application is in the background
           Ti.App.iOS.scheduleLocalNotification({
               alertBody:"Download complete!",
               date:new Date().getTime() 
           });
           progress.hide();
       });
       // Monitor this event to know when all session tasks have completed
       Ti.App.iOS.addEventListener('sessioncompleted', function(e) {
           Ti.API.info("sessioncompleted " + JSON.stringify(e));
           if (e.success) {
               alert("Downloads completed successfully.");
           }
       });
       
  4. Chee Kiat Ng 2016-08-05

    [~jlongton] The error log you are showing seems like it's involved with appc-platform-sdk, may not have anything to do with this ticket. Can you verify: 1. If you are on preprod or prod 2. Try using appc cli core 5.3.1
  5. Josh Longton 2016-08-05

    Verified as fixed, the error is not present now. Tested on:
       iPhone 6s (9.3.1)
       iOS Simulator (9.3)
       
Mac OSX El Capitan 10.11.6
       Studio: 4.7.0.201607250649
       
Ti SDK: 5.4.0.v20160804185318
       
Appc NPM: 4.2.7
       Appc CLI: 5.4.0-40
       Node: v4.4.4
       
    *Closing ticket*

JSON Source