Reputation: 1950
I'm programming a simple iCloud manager for some projects.
Currently I have a small test code to see if my manager successfully evicts files from the devices' local storage (and does not delete them from iCloud storage). To do this, the documentation states that you use:
evictUbiquitousItemAtURL:error:
Removes the local copy of the specified cloud-based item.
My test snippet looks like this:
if ([self evictLocaleFile:@"myTestFile.txt"])
NSLog(@"File was evicted successfully!");
[self listAllLocalFiles];
The idea here being that I first evict the specified file, then list the local files in the local iCloud folder to see if it's still there or not.
The first method simply evicts the specified file like this:
-(BOOL) evictLocaleFile:(NSString *)name
{
NSURL *deletionURL = [[NSURL alloc] initFileURLWithPath:[ubiquityContainerURL path]];
deletionURL = [deletionURL URLByAppendingPathComponent:@"Documents" isDirectory:YES];
deletionURL = [deletionURL URLByAppendingPathComponent:name isDirectory:NO];
NSLog(@"EVICTION URL: %@", deletionURL.path);
NSError* err = nil;
BOOL didEvict = [[NSFileManager defaultManager] evictUbiquitousItemAtURL:deletionURL error:&err];
if (err != nil)
{
NSLog(@"Eviction error: %@", err.description);
}
return didEvict;
}
The method for listing local files looks like this:
-(NSArray*) listAllLocalFiles
{
NSString *localPath = [[ubiquityContainerURL path] stringByAppendingFormat:@"/Documents"];
NSLog(@"PATH: %@", localPath);
NSArray* files = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:localPath error:nil];
for (NSString* fileName in files)
{
NSLog(@"FILE %@", fileName);
}
return files;
}
Now the weird thing is that when I execute the program, it does state that it evicts the file successfully. But yet the file is still listed as existing under the locale files? Shouldn't the file be completely removed and non-existing at this point? I'm not sure why the file is not being removed.
I even attempted to first save the file down, then turn off WIFI and run the code again to make sure it's not because iCloud forces the file to be downloaded again, but this did nothing to solve the problem.
Anyone has an idea of what could be happening?
Upvotes: 3
Views: 1826
Reputation: 1950
So I figured out the problem.
My code was actually evicting the file from my local device correctly. It was due to a misunderstanding of how the ubiquity container actually worked on my behalf. I was under the impression that every app had a local ubiquity container which showed a local state on the current device (so that would mean only files you have downloaded on the testing device), instead the ubiquity container URL used shows all files available to you, whether you've downloaded them on the current device or not.
Upvotes: 3
Reputation: 845
Have you tried NSFileManager
's removeItemAtURL:error:
?
I use this method to remove file from the iCloud, which works fine.
As per the documentation, evictUbiquitousItemAtURL:error:
described as, it removes only the local copy of the specified cloud-based item.
Upvotes: 3