Bug 3229 - NSUbiquitousKeyValueStore Needs Improvement
Summary: NSUbiquitousKeyValueStore Needs Improvement
Alias: None
Product: iOS
Classification: Xamarin
Component: Xamarin.iOS.dll ()
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)

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

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.

Please create a new report for Bug 3229 on Developer Community or GitHub if you have new information to add and do not yet see a matching new report.

If the latest results still closely match this report, you can use the original description:

  • Export the original title and description: Developer Community HTML or GitHub Markdown
  • Copy the title and description into the new report. Adjust them to be up-to-date if needed.
  • Add your new information.

In special cases on GitHub you might also want the comments: GitHub Markdown with public comments

Related Links:

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);