Re: Can't delete file on device
Re: Can't delete file on device
- Subject: Re: Can't delete file on device
- From: Conrad Shultz <email@hidden>
- Date: Tue, 27 Mar 2012 14:16:51 -0700
On 3/27/12 1:50 PM, Rick Mann wrote:
>
> On Mar 27, 2012, at 12:05 , Mike Abdullah wrote:
>
>> Show us some code.
>
> NSURL* url = [NSURL URLWithString: ci.diskURI];
> if ([fm fileExistsAtPath: url.absoluteString])
> {
> NSLog(@"File exists: %@", url.absoluteString);
> }
>
> NSError* err = nil;
> [fm removeItemAtURL: url error: &err];
> if (err != nil)
> {
> NSLog(@"Unable to delete file at %@: %@\n%@", url, err, err.userInfo);
> }
As David already pointed out you should probably be using file URLs.
There are a couple other issues:
1) You shouldn't handle errors by testing (err != nil). This might work
(since you initialized err to nil) assuming that removeItemAtURL: isn't
fiddling with err anyway, but it's not guaranteed. The preferred
approach is to test the return value of removeItemAtURL: (it returns
BOOL for a reason!) and examine err iff the method returns NO.
2) Less likely, but still reasonably plausible since you are working
with cache files (which I imagine have other clearance mechanisms), is
that you are encountering a race condition. It's possible that another
thread removed the file in the intervening time between your
fileExistsAtPath: and removeItemAtURL: calls. This is a major reason
why it's usually not useful to to ask "can I delete a file?" but rather
to try "delete the file!" and handle any errors appropriately.
--
Conrad Shultz
Synthetiq Solutions
www.synthetiqsolutions.com
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden