Bug 3229 - NSUbiquitousKeyValueStore Needs Improvement
Summary: NSUbiquitousKeyValueStore Needs Improvement
Alias: None
Product: iOS
Classification: Xamarin
Component: Xamarin.iOS.dll (show other bugs)
Version: 5.0
Hardware: PC Mac OS
: --- enhancement
Target Milestone: Untriaged
Assignee: Bugzilla
Depends on:
Reported: 2012-02-06 01:36 UTC by t9mike
Modified: 2016-02-08 18:41 UTC (History)
4 users (show)

See Also:
Is this bug a regression?: ---
Last known good build:


Description t9mike 2012-02-06 01:36:43 UTC
The following are suggested improvements for NSUbiquitousKeyValueStore:

1) There are numerous dead end members of this class. For example, 
 ValueForKey() exists in the MT NSUbiquitousKeyValueStore API. No such function exists in the Apple class reference, https://developer.apple.com/library/mac/#documentation/Foundation/Reference/NSUbiquitousKeyValueStore_class/Reference/Reference.html. You get a crash using it. These should not show up in the MT API. 

2) I think ObjectForKey() should be exposed in addition to your [] getter. I kept looking for ObjectForKey() and did not realize you removed it in favor of getter. This is problematic for anyone following Apple docs.

3) [] accepts NSString instead of string. Various other calls in NSUbiquitousKeyValueStore also work this way. Isn't it SOP to hide this and only expose string?

4) Need a proper static event that relays NSUbiquitousKeyValueStoreDidChangeExternallyNotification and exposes C# types in event args for NSUbiquitousKeyValueStoreChangeReasonKey and NSUbiquitousKeyValueStoreChangedKeysKey.

5) Recommend a second example to supplement the current "Cloud" example code. It should include standard best practices for: calling synchronize() at startup, wiring up event event in (3), and doing something to update UI as example when this happens. When I was experimenting with NSUbiquitousKeyValueStore I used a random number generator on my set. That is much more useful when testing between two iOS devices.
Comment 2 Miguel de Icaza [MSFT] 2012-02-19 14:58:29 UTC

ValueForKey comes from the NSValueCoding protocol that is adopted by NSObject.   Some objects might not implement it, but the method must be exposed.

The reason for exposing NSString instead of string in a few places in the APIs is when the APIs consume the pointer to the NSString as a token as opposed to the actual contents of the string.

Some code in Objective-C assumes that you will pass the exact token, so they compare pointers instead of comparing strings:

void IsValid (NSString *key)
    return key == kSomeGlobal1 || key = kSomeGlobal2;

Instead of doing:

void IsValid (NSString *key)
   return [key isEqualToString:kSomeGlobal1] || [key isEqualToString:kSomeGlobal2];

The rest of the feedback is very useful, we see what we can do to make the changes and improve it.

In general, I am not quite happy with APIs that return NSArrays that must be navigated by hand.   It leads to fairly long code paths, we have been discussing a few options, but nothing has jumped as great so far.
Comment 3 Rolf Bjarne Kvinge [MSFT] 2016-02-08 18:41:48 UTC
1) Explained in comment #2.

2) Not sure about this one. The fact that there's a similar sounding method on NSObject (ValueForKey) that doesn't work makes this more confusing, so I think we should add ObjectForKey too. Miguel, what do you think?

3) The indexer now accepts string as well (maccore/master: 01c641aa6a43c117692a8a958326ef9523ff1db0).

4) This has been implemented for a while, use this:

    NSUbiquitousKeyValueStore.Notifications.ObserveDidChangeExternally ((sender, args) => {
        Console.WriteLine ("Observed DidChangeExternally. ChangeReason: {0} ChangedKeys: {1}", args.ChangeReason, args.ChangedKeys);

Note You need to log in before you can comment on or make changes to this bug.