private void OnSubscriptionsFetched(CKSubscription[] subscriptions, NSError error) { if (error != null) { Debug.LogError(error.LocalizedDescription); return; } if (subscriptions.Length > 0) { var subscriptionIds = subscriptions.Select(sub => sub.SubscriptionID.ToString()).ToArray(); Debug.LogFormat("You have the following subscriptions: {0}", string.Join(",", subscriptionIds)); return; } Debug.Log("No subscriptions detected - making a new one"); // Make sure a record of this type exists on the server before attempting // a subscription, or the subscription will fail database.SaveRecord(new CKRecord(recordType), (record, error2) => { if (error2 != null) { Debug.LogError(error2.LocalizedDescription); return; } // You only need to do this ONCE CreateSubscription(); }); }
private void ModifyRecord() { // There's no "modify record" operation in cloud-kit. To modify a record // you just re-save a record whose 'RecordChangeTag' matches the tag on // the server. This ensures that you are always modifying the most recent // version of the record. // For a single client application, this should always be the same // So let's change a few values and re-save the record // Cloudkit stores both fixed and floating percision types in the same // Number type record.SetInt(UnityEngine.Random.Range(0, int.MaxValue), "MyNumber"); record.SetDouble(UnityEngine.Random.Range(0f, float.MaxValue), "MyNumber"); // You can also store arbitrary byte data byte[] bytes = Encoding.ASCII.GetBytes("Test Buffer"); record.SetBuffer(bytes, "TestBufferKey"); // You can't change the type of a field you set previously // This would generate an error... // record.SetInt(0, "MyField"); database.SaveRecord(record, OnRecordModified); }
void Run() { Debug.Log("Example 4 - CKAsset"); database = CKContainer.DefaultContainer().PrivateCloudDatabase; var record = new CKRecord("MyType"); // CloudKit recommends if your data is larger than 1MB that you store // it as an asset... // our example won't be anything close to 1MB, but it'll illustrate the // technique #if UNITY_TVOS string path = Path.Combine(Application.temporaryCachePath, "Asset.bytes"); #else string path = Path.Combine(Application.persistentDataPath, "Asset.bytes"); #endif byte[] bytes = Encoding.ASCII.GetBytes("AssetData"); File.WriteAllBytes(path, bytes); // Assets have to be files, so you pass it the filepath to something // in the user's data directory // the asset will be stored in cloudkit, with a URL for retrieval var fileurl = NSURL.FileURLWithPath(path); record.SetAsset(new CKAsset(fileurl), "MyAsset"); database.SaveRecord(record, OnRecordSaved); }
private void Run() { Debug.Log("Example1 - Hello World"); database = CKContainer.DefaultContainer().PrivateCloudDatabase; var record = new CKRecord("Hello"); record.SetString("Hello World", "Greeting"); database.SaveRecord(record, OnRecordSaved); }
private void Run() { Debug.Log("Example 2 - Basic Database Operations"); database = CKContainer.DefaultContainer().PrivateCloudDatabase; record = new CKRecord("MyType"); record.SetString("An Example", "MyField"); Debug.Log("Saving a record"); database.SaveRecord(record, OnRecordSaved); }