Alessandro
Alessandro

Reputation: 4100

Using KVO to track a change in count of NSArray

My app uses multi peer connectivity to send files to other devices in IOS7. With this framework more than one device can connect to the MCSession, and there is a specific array that gives us the count of the number of devices connected. I want to be notified when there is a change to this count, but I don't seem to understand how to make the array KVO compliant, even if I so numerous internet links and questions. The problem is that this array is controlled externally:

[session connectedPeers];

To get the count I do:

NSArray* array = [session connectedPeers];
array.count

But it is not the array which changes, rather the [session connectedPeers];

How can I track this change?

Upvotes: 0

Views: 830

Answers (2)

emma ray
emma ray

Reputation: 13620

According to the docs: ([NSArray Class reference])1

addObserver:forKeyPath:options:context:

NSArray objects are not observable, so this method raises an exception when invoked on an NSArray object. Instead of observing an array, observe the to-many relationship for which the array is the collection of related objects.

I'd suggest using a MCSessionDelegate method as mentioned by Wain.

Upvotes: 1

Wain
Wain

Reputation: 119031

MCSessionDelegate provides the session:peer:didChangeState: method which should notify you when any peer connects or disconnects from the session.

Upvotes: 0

Related Questions