This repository has been archived by the owner on Nov 20, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 42
/
Copy pathns-CloudKit.xml
93 lines (79 loc) · 6.83 KB
/
ns-CloudKit.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
<Namespace Name="CloudKit">
<Docs>
<summary>The CloudKit namespace provides access to iCloud.</summary>
<remarks>
<para>The CloudKit namespace allows application developers to control the flow of data between their applications and iCloud. iCloud provides server-side data storage, both "public" that is shared between all instances of an application or specific applications and "private" that is unique to a single user. iCloud storage and transport is encrypted and synchronized between Apple devices. CloudKit does not provide client-side persistence or data caching -- CloudKit functionality is only available while the user has an active Internet connection.</para>
<para>The highest-level class in the CloudKit is <see cref="T:CloudKit.CKContainer" />. A CloudKit Container (sometimes referred to in Apple documentation as a "Ubiquity container") is an information store identified with a particular name. Apple advises using a name of the form "iCloud.{reverse DNS}.{appName}", for instance, "iCloud.com.mycompany.MyApp". Developers must create and configure their containers using the iCloud Dashboard, available by way of the Apple developer portal.</para>
<para>A single <see cref="T:CloudKit.CKContainer" /> may contain both public data, which is shared between all instances of the app (see <see cref="P:CloudKit.CKContainer.PublicCloudDatabase" />), and private data, which contains user-specific data (see <see cref="P:CloudKit.CKContainer.PrivateCloudDatabase" />). Within iCloud, public data is stored in the app's iCloud storage while private data is stored in the user's private iCloud storage. The public <see cref="T:CloudKit.CKDatabase" /> is available to all connected users of the app, whether or not they are logged in to iCloud. The private <see cref="T:CloudKit.CKDatabase" /> is only if the user is logged in to iCloud. Developers must write their code such that it reacts gracefully to changes in the user's login or connection status.</para>
<para>These public and private <see cref="T:CloudKit.CKDatabase" /> objects store structured records. These are represented by <see cref="T:CloudKit.CKRecord" /> objects. In addition to field-like key-value data, these records may hold <see cref="T:CloudKit.CKAsset" /> large objects either directly (retrieved with the containing <see cref="T:CloudKit.CKRecord" />) or as references (allowing lazy retrieval of the large data).</para>
<para>All <see cref="T:CloudKit.CKRecord" /> instances are stored within a <see cref="T:CloudKit.CKRecordZone" />. A <see cref="T:CloudKit.CKRecordZone" /> is something like a traditional database instance: it can contain multiple <see cref="T:CloudKit.CKRecord" /> stores that may refer to each other but not to stores in other <see cref="T:CloudKit.CKRecordZone" /> instances and <see cref="T:CloudKit.CKRecordZone" /> objects can be used to coordinate atomic operations across multiple <see cref="T:CloudKit.CKRecord" /> stores. The similarity to a database instance is not universal; for instance, a developer may use <format type="text/html"><a href="https://docs.microsoft.com/en-us/search/index?search=M:CKDatabase.FetchRecordAsync(CKRecordID)&scope=Xamarin" title="M:CKDatabase.FetchRecordAsync(CKRecordID)">M:CKDatabase.FetchRecordAsync(CKRecordID)</a></format> to retrieve a <see cref="T:CloudKit.CKRecord" /> directly from the <see cref="T:CloudKit.CKDatabase" /> without referring to the zone. The default <see cref="T:CloudKit.CKRecordZone" /> is available via <format type="text/html"><a href="https://docs.microsoft.com/en-us/search/index?search=P:CloudKit.CKRecordZone.DefaultRecordZone()&scope=Xamarin" title="P:CloudKit.CKRecordZone.DefaultRecordZone()">P:CloudKit.CKRecordZone.DefaultRecordZone()</a></format>. Developers may create additional zones using the CloudKit dashboard and use them to partition their data logically.</para>
<para>CloudKit apps require a provisioning profile with the <c>com.apple.developer.icloud-services</c> entitlement and an app-specific Bundle Identifier (e.g., "com.mycompany.CloudKitApp"). Developers must create and configure such an provisioning profile.</para>
<para>The following demonstrates some typical uses of CloudKit that work on single records:</para>
<example>
<code lang="csharp lang-csharp"><![CDATA[
//Create
private async Task<CKRecord> StoreInCloudKit (string name, CLLocation loc, int stationId)
{
string containerName = "iCloud.com.xamarin.CKWork";
var container = CKContainer.FromIdentifier (containerName);
CKDatabase publicDatabase = container.PublicCloudDatabase;
var zoneId = CKRecordZone.DefaultRecordZone ().ZoneId;
var recordType = "StationReferences";
var record = new CKRecord (recordType);
record ["Name"] = new NSString (name);
record ["Position"] = loc;
record ["StationID"] = new NSNumber (stationId);
var storedRecord = await publicDatabase.SaveRecordAsync (record);
return storedRecord;
}
//Retrieve (via ID)
private async Task<CKRecord> AccessCloudKit ()
{
string containerName = "iCloud.com.xamarin.CKWork";
var container = CKContainer.FromIdentifier (containerName);
CKDatabase publicDatabase = container.PublicCloudDatabase;
var recordNameIHappenToKnow = "7eaf0432-2fa4-475f-851a-c6a19b3f8587";
var recordId = new CKRecordID (recordNameIHappenToKnow);
var record = await publicDatabase.FetchRecordAsync (recordId);
return record;
}
//Retrieve (query)
private async Task<CKRecord []> QueryCloudKit ()
{
string containerName = "iCloud.com.xamarin.CKWork";
var container = CKContainer.FromIdentifier (containerName);
CKDatabase publicDatabase = container.PublicCloudDatabase;
var zoneId = CKRecordZone.DefaultRecordZone ().ZoneId;
Console.WriteLine ($"Default zone name = '{zoneId.ZoneName}' owner = '{zoneId.OwnerName}'");
/*
var zoneName = "_defaultZone";
var ownerName = "__defaultOwner__";
var zoneId0 = new CKRecordZoneID (zoneName, ownerName);
Console.WriteLine ($"Assert { zoneId0 == zoneId }");
*/
var recordType = "StationReferences";
var location = new CLLocation (19.7303, -155.056);
var predicate = NSPredicate.FromFormat ("distanceToLocation:fromLocation:(Position, %@) < 100", location);
var query = new CKQuery (recordType, predicate);
var result = await publicDatabase.PerformQueryAsync (query, zoneId);
return result;
}
//Update
private async Task<CKRecord> UpdateInCloudKit (CKDatabase database, CKRecord record, NSString key, NSObject newValue)
{
record [key] = newValue;
var modifiedRecord = await database.SaveRecordAsync (record);
return modifiedRecord;
}
//Delete
private async Task<CKRecordID> DeleteRecord (CKDatabase database, CKRecord record)
{
var id = record.Id;
var deletedId = await database.DeleteRecordAsync (id);
return deletedId;
}
]]></code>
</example>
</remarks>
</Docs>
</Namespace>