Search code examples
swifticlouduidocument

saveToURL in UIDocument returns false


I am executing this function to save an UIDocument both with iCloud enabled and disabled but I seem to miss on both throws. In particular when iCloud is eanbled saveToURL return false, and I have no idea why it so, given the document is not nil and the path a reasonable one; this is my function in brief:

func newFavorite(favorite: palinaModel) ->Bool {
    if indexForPalina(favorite) != nil {
        print("stop already favorite")
        return false;
    }
// Save Stop
    let baseURL = NSFileManager.defaultManager().URLForUbiquityContainerIdentifier(nil)
    var favoriteURL:NSURL!
    if (baseURL != nil) {
        let favoritesURL = baseURL!.URLByAppendingPathComponent(pathComponent())
        favoriteURL = favoritesURL.URLByAppendingPathComponent(String(format:"Stop_%@", favorite.palina))
    } else {
        let filePath = getFilePath()
        favoriteURL=NSURL(fileURLWithPath: filePath)
    }
    let document = FavoriteStopDocument(fileURL:favoriteURL, favorite:favorite)

// Add Bookmark To Bookmarks
    self.favoriteElements.append(document)
    print("favoriteUrl=" + favoriteURL.path!+" document="+document.favoriteStop!.palina)
    document.saveToURL(favoriteURL, forSaveOperation:.ForCreating, completionHandler:{(success) in
        if (success) {
            print("Save succeeded.");
        } else {
            print("Save failed.");
        }
    })

    return true
}

Thanks for you support.


Solution

  • I ended up dumping UIDocument after having even possibly waster a ticket with Apple, and decided to save the dictionary as a normal NSUserDefault.