如果JSON文件是从网上下载的,保存到内部存储器,然后从内部存储器中读取,它会更新我的tableview就好了。但是,如果我关闭应用程序并将其重新启动,因此文件已存储到手机中,则不会更新tableview。但是,无论文件是否刚下载,我都从同一方法读取JSON文件。也就是说,如果我下载它,我将其保存到存储中,然后从存储中读取它。这是代码:
- (id)init
{
if ((self = [super init])) {
NSString *filePath = [self getFilePath];
if (![[NSFileManager defaultManager] fileExistsAtPath:filePath]) { // if file does not exist, create it.
[self startBackgroundDownloadOfJSONFile];
NSLog(@"file does not already exist");
} else {
[self readJSONFileFromInternalStorage];
NSLog(@"file already exists");
}
}
return self;
}
- (NSString *)getFilePath
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [NSString stringWithFormat:@"%@/%@", documentsDirectory, @"bookData.json"];
//NSLog(@"filePath %@", filePath);
return filePath;
}
- (void)startBackgroundDownloadOfJSONFile
{
dispatch_async(kBgQueue, ^{
self.data = [NSData dataWithContentsOfURL:myURL]; //URL is defined elsewhere
[self performSelectorOnMainThread:@selector(fetchedData:)
withObject:self.data waitUntilDone:YES];
});
NSLog(@"attempting to download json from web");
}
- (void)readJSONFileFromInternalStorage
{
NSLog(@"attempting to read from internal storage");
NSString *filePath = [self getFilePath];
NSData *jsonData = [[NSFileManager defaultManager] contentsAtPath:filePath];
NSError* error;
NSDictionary *book;
book = [NSJSONSerialization
JSONObjectWithData:jsonData
options:kNilOptions
error:&error];
self.name = [book objectForKey:@"name"];
self.items = [[NSMutableArray alloc] initWithCapacity:20];
NSArray *chapters = [book objectForKey:@"chapters"];
for (NSDictionary *chapter in chapters) {
CompanyProtocolItem *item = [[CompanyProtocolItem alloc] init];
item.name = [chapter objectForKey:@"name"];
NSLog(@"%@", [chapter objectForKey:@"name"]);
item.color = [chapter objectForKey:@"color"];
NSArray *pages = [chapter objectForKey:@"pages"];
for (NSDictionary *page in pages) {
ProtocolFile *chapterPage = [[ProtocolFile alloc] init];
chapterPage.name = [page objectForKey:@"name"];
chapterPage.color = [page objectForKey:@"color"];
chapterPage.path = [page objectForKey:@"path"];
[item.items addObject:chapterPage];
}
[self.items addObject:item];
NSLog(@"%lu",(unsigned long)[self.items count]);
}
[[NSNotificationCenter defaultCenter]
postNotificationName:@"mainDataLoaded"
object:self];
}
- (void)fetchedData:(NSData *)responseData {
[self writeData];
[self readJSONFileFromInternalStorage];
}
- (void)writeData
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [NSString stringWithFormat:@"%@/%@", documentsDirectory, @"bookData.json"];
//NSLog(@"filePath %@", filePath);
NSString *helloStr = @"hello world";
NSError *error;
[helloStr writeToFile:filePath atomically:YES encoding:NSUTF8StringEncoding error:&error];
//NSString *jsonPath=[[NSSearchPathForDirectoriesInDomains(NSUserDirectory, NSUserDomainMask, YES) objectAtIndex:0] stringByAppendingFormat:@"/data.json"];
[self.data writeToFile:filePath atomically:YES];
}
每个循环内的日志显示已读取项目。此外,[self.items count]返回正确数量的项目。当我重新启动应用程序(下载文件)时,我将该数据传递给tableview控制器,它说没有项目。但是,如果我卸载了应用程序,它会正确地转到Web上,下载文件,将其保存到内部存储器,读取它并将对象传递给tableview,tableview会按预期显示项目和更新。
发生了什么事?