Search code examples
iosxamarinxamarin.iosicloud

Xamarin iOS: Can't fetch record from private database in icloud


Want to save a asset to the private database but.... ...my output says every launch "creating file...", so where's the problem at my code? :( (uploading/saving seems to work because "uploaded" appears)

    var recordID = new CKRecordID("diary");
    AppleHelper.PrivateDatabase.FetchRecord(recordID, (record, err) => {

    // Was there an error?
    if (err != null) {
        Console.WriteLine(err.ToString());
        Console.WriteLine("creating file...");

        var dbName = "diary.db";
        var dbPath = Path.Combine(documents.Path, dbName);
        AppDelegate.lib.Conn = new SQLite.SQLiteConnection (dbPath);
        AppDelegate.lib.Conn.CreateTable<LibChemotherapie.Diary> ();

        if (File.Exists (dbPath)) {

            var assetRecord = new CKRecord ("sqlite");
            var db = new CKAsset(NSUrl.CreateFileUrl(new string[] { dbPath }));
            assetRecord ["diary"] = db;

            AppleHelper.PrivateDatabase.SaveRecord (assetRecord, (record2, err2) => {

                if(err2 == null)
                    Console.WriteLine("uploaded");

            });

        }
    }
    else {

        Console.WriteLine("yeah exists");

    }
    });

Solution

  • Thank you, I solved the problem. I could not fetch the record that I stored. The problem was, that I used not the RecordID for loading, which I used for saving.