0

我正在使用以下代码下载 slqite 文件并存储它

self.responseData = NSMutableData

我收到responseData = 2048bytes。运作良好。

然而,白写它确实会创建一个文件myFile.sqlite,但它是零字节的。怎么了?

- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSString *dbPath = [self getDBPath];
    BOOL success = [fileManager fileExistsAtPath:dbPath];
    if(!success)
    {
        if(self.responseData)
        {
            dbPath = [dbPath stringByAppendingPathComponent:[self.selectedBtn.dbPath lastPathComponent]];
            [self.responseData writeToFile:dbPath atomically:YES];
            [self performSegueWithIdentifier:@"list" sender:self];
        }
    }

    [self.alertView removeFromSuperview];
}

-(NSString *) getDBPath
{
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory , NSUserDomainMask, YES);
    NSString *documentsDir = [paths objectAtIndex:0];
    NSURL *url = [[NSURL alloc] initWithString:[self.selectedBtn.dbPath lastPathComponent]];
    return [documentsDir stringByAppendingPathComponent:[url lastPathComponent]];
}

我收到错误

`The operation couldn’t be completed. (Cocoa error 4.)`

dbPath : /Users/umar/Library/Application Support/iPhone Simulator/6.1/Applications/00027635-CE9C-48C3-8000-64CA1E6532F1/Documents/music.sqlite/music.sqlite
4

2 回答 2

1
Documents/music.sqlite/music.sqlite

不,这不是一条有效的路径......你的意思是Documents/music.sqlite,不是吗?另外,我不明白你为什么要NSURL为不相关的任务强奸穷人。

return [documentsDir stringByAppendingPathComponent:[self.selectedBtn.dbPath lastPathComponent]];

另外,确保self.selectedBtn.dbPath也确实是一条有效的路径而不是垃圾(我可以想象它是)。

于 2013-03-14T20:10:51.877 回答
1

您要追加[self.selectedBtn.dbPath lastPathComponent]两次,一次是 in getDBPath,一次是 ion connctionDidFinishLoading

选择其中一个要删除的实例。

于 2013-03-14T20:12:23.730 回答