tvOS GameController At what point does GCController.controllers become populated after launch? - objective-c

I'm trying to read the GCController.controllers() array after my app has launched to know which controllers were already connected to the AppleTV at app launch. But GCController.controllers().count is 0 until some point after viewDidAppear gets called on my initial UIViewController. Does anyone know the definitive point by which you can check GCController.controllers() to know that it has been populated with the currently connected controllers?
I am aware of the need to register for controller connection notifications with;
NSNotificationCenter.defaultCenter().addObserver(self, selector: "handleControllerDidConnectNotification:" , name: GCControllerDidConnectNotification , object: nil)
But that is for later after launch. First I need to know which ones are already connected. Anyone know?

You can call startWirelessControllerDiscoveryWithCompletionHandler on viewDidLoad and then check GCController.controllers() on viewWillAppear that seem to work for the game app I just finished.
Docs:
After your app has finished launching, the operating system
automatically creates a list of connected controllers. Call the
controllers class method to retrieve an array of GCController objects
for all connected controllers. Next, use these objects to configure
the controllers or read the controller’s inputs. If there are no
connected controllers or you call this method while your app is
launching, the array will be empty.

GCController will generate GCControllerDidConnectNotification notifications for each controller, including those connected to the device prior to launch. If you're not getting notifications for already-connected controllers, confirm the following:
Double-check that it is paired and turned on
Make sure it is a MFi controller.

Related

Application crashes while logging out in iPAD

My question is specific to iPAD, and I also aware of the basic memory management of iOS, but I am having a different problem.
As I have build an application where I have several UIViewControllers and UIViews,
I have a loginController thats gets called when I launch the App.
My MainView is a single screen with all the ViewController loaded and placed at their respective places and the app runs fine and smoothly.
Problem:
Problem comes when I logout, most of the time my App crashes by saying EXC_BAD on the
[super dealloc] line of my mainView controller.
As for now on I have added a custom function cleanUP in all my viewControllers that gets called when user logout from the app.
Is this the right approach ?
As I know that we can clean up in our didload etc. function and the dealloc gets called too.
but here i have an iPAD when my all viewControllers are just open in front of me, They will be closed or not visible when I logout from the App.
So how to approach on my crash issue and How to manage memory here in my iPAD?
The best way I know to resolve bad-access problems is to use Instruments with the Zombie tool. As you probably know, when you get a bad access issue, is because you try to access to an object that is deallocated.
Try go to Product -> Profile and choose Zombie. Hit record and reproduce your crash. then inspect the pointer to the object that produced that crash and look for the retain count.

Connecting App Delegate to Controller Class

I'm stuck on this step of developing an XCode application: https://developer.apple.com/library/mac/#documentation/General/Conceptual/Mac101/Articles/07_WhereNext.html
What I'm confused about is the role of the AppDelegate class that's created by default when creating a project initially in XCode 4. If I implement a custom controller, let's use the example link above and call it TrackController. Do I make that controller the app delegate? It seems any tutorial I read isn't very clear on this.
So, in AppDelegate, do I create a new instance of the controller class here? And if so, do I then hook up the outlet here? Or do I change the File's Owner to be the controller class? If that's the case, then how's that done in XCode 4?
Not a very well defined question I know, but I'm sure someone knows what I'm talking about.
EDIT
The following screenshot shows what I mean. The blue box is now "Track Controller". I've shown the AppDelegate class in this case though to make it clear. What do I use, AppDelegate.m, or TrackController.m? How should it be done?
EDIT 2
Going back to this, I've uploaded the code to GitHub here as I still haven't figured out how to hook everything up.
To re-explain, I need to figure out how AppDelegate and TrackController all relate and communicate with one another. The concepts are generally new to me and I've read the Apple documentation but they haven't really helped me. I've even had in-depth discussions with experts and I still fail to see how it should work.
My model is Track, then I have TrackController and of course AppDelegate. AppDelegate is instantiating TrackController and I was hoping that it'd be able to open up its window.
What am I doing wrong? Hopefully an expert can help me!
What it's describing is that the application delegate should focus on handling the delegate calls sent to the app.
You actually create a new class to act as the primary controller of the app, and then you add code to call it when the app is finished loading. From there on out, that app is (supposedly) your primary control point (except, of course, when it 'hands off' to another class).
I'm more familiar with how the process works on iOS than Macs, which have to worry about windows, but for an iOS app you'd add code to call your starting view controller from inside ApplicationDidFinishLaunching:WithOptions:. From there, the view controller takes off, and the easiest way to view the entire process is that you start at the view controller (that's how most tutorials handle it, in fact, letting XCode handle the fact that the app delegate creates that view controller).

iOS Airplay viewcontroller data sychronisation

I've built an iOS 5 iPad app which makes use of a second screen. We have an admin view (on the iPad) and an external view through an HDMI enabled TV connected via the Apple DVI adapter. Both the iPad view and the TV view get the same data updates from a service call which is made every few seconds. We then present the data received as a series of charts; the charted data is presented very differently for the TV and iPad views - but the core dictionary of data is the same. I'm wondering about an elegant way to architect this solution. At the moment I have one of the view controllers (the admin iPad VC) doing the service calls using GCD and then dispatching NSNotifications which update the data (charts) properties on the other (TV) view controller. I'm considering moving the service calls away from the VC and creating a singleton which is initialized in the app controller. I then (somehow) set the two VCs as delegates and they get updated using a simple protocol. I'm not entirely sure if this is a good approach or if I should consider something else? Can I even set both VCs as the delegates of another class or is it typically only one delegate per class instance?
Thanks for any input.
Ben
Why not abstract the chart data into its own model class, which you can share in both view controllers? The model class can be responsible for fetching the new data. To make the controllers aware of updates, they can either use KVO on the model object, or they can observe notifications sent from the model object when an update occurs, or you can have an array of delegates for the model object and each view controller can be a delegate.
There doesn't seem to be any compelling reason to make it a singleton, although you can if you really want.

What am I doing wrong with NSManagedObjectContext dependency injection?

I am trying to use NSManagedObjectContext dependency injection as recommended by Marcus Zarra -- I'm creating an M.O.C. in my AppDelegate and passing it as a retained property to each of my view controllers.
Generally this seems to work well, but in a modal table view controller that presents data via an NSFetchedResultsController, I only see what was in the database when the app was launched. That is, if the user adds data at runtime, it gets added correctly to the database, but does not appear when the modal ViewController is opened and the NSFetchedResultsController is created (using the injected NSManagedObjectContext). However, if I close the app and restart, then open the modal view controller, I do see the data added in the previous session.
Do I have to refresh the M.O.C. in some way prior to creating the NSFetchedResultsController? I am absolutely sure that the modal view controller and the NSFetchedResultsController are being created, and the fetch is being executed, AFTER the new user data has been entered.
To start, you should log the moc in both app delegate and your view controller to confirm that the moc in both places has the same address and is therefore the same object.
If it is, then most likely you've got an issue with the FRC's cache. Set the cache to nil and/or refresh the cache and see if that resolves it.

What message is sent to a UIView when the application terminates?

I am trying to locate a message which I can override and then save changes to my application.
On MainWindow.xib I have placed a UIView and set its class (in interface builder) to be my Custom view TouchDrawView.
In TouchDrawView I have a bunch of code for handling touch events and 2 arrays which track these touch events.
My application is launched by the AppDelegate but it has no reference to this TouchDrawView. It simply launches the application.
What I want to do is save my 2 arrays when the application terminates - I can do this in the TouchDrawView but I don't know what message this UIView gets sent when the whole application is about to terminate and I can't do it in the AppDelegate because it doesn't have a reference to the 2 array or the custom UIView
UIView instances will not get send any messages when the app will terminate.
There's another easy way to get notified of app state changes: notifications. You can register for notifications sent through [NSNotificationCenter defaultCenter].
For older versions of iPhone OS there's a UIApplicationWillTerminateNotification. Beginning from iOS 4 you should also listen for UIApplicationDidEnterBackgroundNotification, to prepare for termination.
The short, unhelpful answer is that a UIView is not sent a message when the application terminates.
You need to think in terms of Model-View-Controller because that's how Cocoa and Cocoa Touch are designed. Of those three, if a message was directly sent to any it would be the controller. In your case that would be the UIViewController that talks to your view.
The bad news is there is no such message there either.
An application shuts down, not an individual screen/view.
There are (at least) two ways of doing what you want to do. Firstly, you could save the state of the view controller when it is released. (That kind of feels wrong to me.)
They way that I do it in my app is to listen for the applicationWillTerminate message in your UIApplicationDelegate and traverse the view hierarchy and save the state of each view controller. When the system starts up you can do the opposite. I blogged about it here.