2

Helo

Before updating Xcode to the 7.3 version I had an app with an WatchOS 2 app, The watch app would call the func session(session: WCSession, didReceiveUserInfo userInfo: [String : AnyObject]) { and the iOS app would pick the call and insert the passed value. All was fine.

But since updating to Xcode 7.3 one issue i noticed, the func session(session: WCSession, didReceiveUserInfo userInfo: [String : AnyObject]) { is being called twice ONLY when the iOS app is launched for the first time, if the app is running or is in the background, that function is only called once.

If I pass the values 1, 5, and 10 and the iOS app is not running, the values 1, 5, 10, 1, 5, and 10 are added. But if the app is running in any form, the values 1, 5, and 10 are added.

Any idea why?

Here is the code from the WatchOS side, I did think of that myself, but according to my tests they are only called once. I have done many tests, and this is only happening when the iOS app is launched, not when its ruling in the background.

@IBAction func ConfirmButtonPressed() {

    let applicationDict = ["Amount out":  self.AmountText    ]// Create a dict of application data
      //applicationDict = ["status":   "0"   ]// Create a dict of application data
    WCSession.defaultSession().transferUserInfo(applicationDict)
}

Here is the iOS app code from the app delegate

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {

    if (WCSession.isSupported()) {
        print("xyz3")
        session = WCSession.defaultSession()
        session.delegate = self
        session.activateSession()
    }

..........

func session(session: WCSession, didReceiveUserInfo userInfo: [String : AnyObject]) {

    var status = false
    var AmountUILabel = ""

          status = false
         AmountUILabel  = userInfo["Amount out"]  as! String
        print(userInfo["Amount out"]  )

    let i  =  NSString (string:   AmountUILabel ).doubleValue
      let when = NSDate()
     let list :[AnyObject] =  controller.viewControllers!
    let j = list[1].topViewController  as! AllEntriesTableViewController

    j.AddAmount(i , date:  when, what: "---", status: status)
   }
hsn
  • 173
  • 2
  • 10
  • Can you show what the watch app is sending? Does the dictionary have 3 values, and you are transferring it twice? –  Mar 25 '16 at 04:22

1 Answers1

2

I was able to figure out the answer after a whole day of research. I should have started the didReceiveUserInfo with dispatch_async

That fixed it and increased up the communication speed between the watch app and the iOS one.

func session(session: WCSession, didReceiveUserInfo userInfo: [String : AnyObject]) {
    dispatch_async(dispatch_get_main_queue()) {
hsn
  • 173
  • 2
  • 10
  • Thanks for coming back to post your solution. I was struggling with the same issue. This video (https://developer.apple.com/videos/play/wwdc2015/713/) was really helpful for understanding WatchConnectivity, and even though they mention these delegates are called outside the main queue I never thought that could cause the duplicate call. – gohnjanotis May 05 '16 at 17:37
  • I had an issue in my iOS app after passing userInfo from the Watch OS 2 app while the iOS app was terminated (force-quitted): the object from the Watch app was created twice when the iOS app launched later; wrapping `didReceiveUserInfo` it in a `dispatch_async ` as suggested here fixed the duplication problem. – cdf1982 Jul 17 '16 at 08:39
  • 1
    @cdf1982 glad that my answer helped! – hsn Jul 18 '16 at 06:14