12

I'm working on an iOS today widget and things are quite functional. However, when running on a real iOS device I occasionally see: host connection <NSXPCConnection: 0x538ee0> connection from pid 42 invalidated

This doesn't seem to be a major problem except that the widget seems to almost reload when this happens, occasionally leaving the space in the Today screen blank until iOS redraws it and everything gets reloaded.

Has anyone else encountered this? I'm not using NSXPCConnection at all in my App, so I'm assuming this is something built into the framework but I'm not sure

ryno2019
  • 313
  • 2
  • 7

2 Answers2

9

NSXPCConnection API is used to perform interprocess connection between Xcode client and your app on iPhone. So you do not need to worry about this one.

Link: https://developer.apple.com/library/mac/documentation/MacOSX/Conceptual/BPSystemStartup/Chapters/CreatingXPCServices.html

So there may be 2 reasons that your widget is terminated.

  1. You need to call completionHandler(NCUpdateResultNoData); right after your widgetPerformUpdateWithCompletionHandler has been called even when the response hasn't been returned.

  2. Your app is terminated because of the automatic app termination. It terminates the widgets/apps for 2 reasons:

a. It terminates apps that are not being used and allowing the reclamation of resources such as memory.

b. It terminates widgets that use too much memory.

bllakjakk
  • 5,045
  • 1
  • 18
  • 28
  • Agreed, but my widget isn't getting any memory warnings, and there seems to be a close relationship between the mentioned host connection message and the widget reloading. – ryno2019 Feb 06 '15 at 13:34
  • probably you had idle state for few moments while debugging, so debugger decided to close connection and reclaim the memory. – bllakjakk Feb 08 '15 at 10:10
  • My widget is facing the same issue. I don't seem to get any memory warning either. The widget is running at around 5mb memory wise (does seem fairly high). – bencallis Feb 12 '15 at 00:14
5

Finally figured out the issue I was having with my widget. It turns out it was nothing to do with the NSXPCConnection but rather a misunderstanding about the Widget Life Cycle on my behalf.

From the documentation I thought that the today view would keep a 'snapshot' of my widgets state until the widgetPerformUpdateWithCompletionHandler method completion handler was called with success.

This does not seem to be the case. From what I can see the 'snapshot' is just used when the Today View is animating in (when the user pulls down the notification centre). As soon as the today view is loaded and stationary your widget is loaded from scratch (inflated from xib if using) and viewDidLoad is called. At this moment you should populate you widget with cached data (not from a web request). If you don't you will see temporary data from your nib. This is what causes the flashing.

When viewDidLoad is complete widgetPerformUpdateWithCompletionHandler is called which allows you to fetch fresh data. When the fresh data is fetched you should call the completion handler and cache the data it so it can be used when the widget is loaded later on from scratch (in viewDidLoad).

A simple way to cache the data is in user defaults.

Hope this helps.

bencallis
  • 3,478
  • 4
  • 32
  • 58