0
votes

I am trying to save a Dictionary of objects to iCloud but when I do it method saveToURL:forSaveOperation: completionHandler: fails. I also tried to override:

- (BOOL)writeContents:(id)contents
    andAttributes:(NSDictionary *)additionalFileAttributes
      safelyToURL:(NSURL *)url
 forSaveOperation:(UIDocumentSaveOperation)saveOperation
            error:(NSError **)outError

and of course the super call also returns false. Yet, I would have liked to read the error, but when I try to have the localizedError the compiler reports an error claiming it is not a structure or union. This is the full piece of code:

-(instancetype)initWithSingleton{
NSURL *ubiq = [[NSFileManager defaultManager]
               URLForUbiquityContainerIdentifier:nil];
NSURL *ubiquitousPackage = [[ubiq URLByAppendingPathComponent:
                             @"Stops"] URLByAppendingPathComponent:kFILENAME];
NSLog(@"file url=%@", ubiquitousPackage);
self=[self initWithFileURL:ubiquitousPackage];
if (self!=nil){
    self.favoriteStops=[[NSMutableDictionary alloc] init];
    NSURL *ubiq = [[NSFileManager defaultManager]
                   URLForUbiquityContainerIdentifier:nil];
    if (ubiq) {
        NSLog(@"iCloud access at %@", ubiq);
        [self loadDocument];
    } else {
        NSLog(@"No iCloud access");
    }
}
return self;
}
#define kFILENAME @"favorite.dox"

- (void)loadData:(NSMetadataQuery *)query {

if ([query resultCount] == 1) {
    NSMetadataItem *item = [query resultAtIndex:0];
    NSURL *url = [item valueForAttribute:NSMetadataItemURLKey];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    NSURLResponse *response;
    NSData *GETReply= [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:nil];
    NSMutableDictionary* dict=[NSKeyedUnarchiver unarchiveObjectWithData:GETReply];
    [self setFavoriteStops:dict];
    NSLog(@"favorites: %@", favoriteStops);
    [self openWithCompletionHandler:^(BOOL success) {
        if (success) {
            NSLog(@"iCloud document opened");
        } else {
            NSLog(@"failed opening document from iCloud");
        }
    }];
}
}

- (void)queryDidFinishGathering:(NSNotification *)notification {

NSMetadataQuery *query = [notification object];
[query disableUpdates];
[query stopQuery];

[[NSNotificationCenter defaultCenter] removeObserver:self
                                                name:NSMetadataQueryDidFinishGatheringNotification
                                              object:query];

_query = nil;
[self loadData:query];

}
- (BOOL)loadFromContents:(id)contents ofType:(NSString *)typeName
               error:(NSError **)outError
{
if ([contents length] > 0) {
    [self setFavoriteStops:[NSKeyedUnarchiver unarchiveObjectWithData:contents]];
}
return YES;
}

- (BOOL)writeContents:(id)contents
    andAttributes:(NSDictionary *)additionalFileAttributes
      safelyToURL:(NSURL *)url
 forSaveOperation:(UIDocumentSaveOperation)saveOperation
            error:(NSError **)outError{
//logging
NSString *str;
str= [[NSString alloc] initWithData:contents encoding:NSUTF8StringEncoding];
NSLog(@"saving data %@", str);
//logging
NSMutableDictionary *dict=[NSKeyedUnarchiver unarchiveObjectWithData:contents];
NSLog(@"dict=%@", dict);

BOOL success= [super writeContents:contents
        andAttributes:additionalFileAttributes
          safelyToURL:url
     forSaveOperation:saveOperation
                error:outError];
NSLog(@"error :%@", outError.localizedDescription) //syntax error
return success;
}

-(void) save{
NSLog(@"file url=%@", [self fileURL]);
[self saveToURL:[self fileURL]
forSaveOperation:UIDocumentSaveForOverwriting
completionHandler:^(BOOL success) {
  if (success) { //this returns false
      [self openWithCompletionHandler:^(BOOL success) {
          NSLog(@"new document saved on iCloud");
      }];
  } else {
      NSLog(@"error in iCloud Saving");
  }
 }];
}

- (id)contentsForType:(NSString *)typeName error:(NSError **)outError
{
NSLog(@"favorite stops=%@ class=%@", self.favoriteStops, [favoriteStops class]);
NSData *archivedData=[NSKeyedArchiver archivedDataWithRootObject:self.favoriteStops];
return archivedData;

}

When I log the url on which to save, that is:

file:///private/var/mobile/Library/Mobile%20Documents/iCloud~com~information~inArrivo/Stops/favorite.dox

And when I check the error on the debugger it is:

Error Domain=NSCocoaErrorDomain Code=4 "The operation couldn’t be completed. (Cocoa error 4.)" UserInfo=0x17bd6cb0 {NSFileNewItemLocationKey=file:///private/var/mobile/Applications/445778BF-86AF-4DE3-9E1B-BAC8F79D14D0/tmp/(A%20Document%20Being%20Saved%20By%20In%20Arrivo%20HD)/favorite.dox, NSFileOriginalItemLocationKey=file:///private/var/mobile/Library/Mobile%20Documents/iCloud~com~information~inArrivo/Stops/favorite.dox, NSUnderlyingError=0x17bfa860 "The operation couldn’t be completed. (Cocoa error 4.)", NSURL=file:///private/var/mobile/Library/Mobile%20Documents/iCloud~com~information~inArrivo/Stops/favorite.dox}

How may I fix it or at least know more?

1

1 Answers

0
votes

The TSI Apple team answered me and provided me with a working class. Upon checking their version with mine the change seems to boil down to an update to - (void)queryDidFinishGathering:(NSNotification *)notification; by adding:

if (query.results == 0)
    {
        [self save];  // no favorites file, so create one
    }

to make it as follows:

- (void)queryDidFinishGathering:(NSNotification *)notification {

    NSMetadataQuery *query = [notification object];
    [query disableUpdates];
    [query stopQuery];

    [[NSNotificationCenter defaultCenter] removeObserver:self
                                                name:NSMetadataQueryDidFinishGatheringNotification
                                              object:query];
//•• added
if (query.results == 0)
    {
        [self save];  // no favorites file, so create one
    }

   [self loadData:query];

   _query = nil;
}