저는 현재 상당히 큰 아이폰 애플리케이션을 만들고 있습니다. 어쨌든 내가 예상했던 것보다 더 큰. 하지만 요점은 응용 프로그램의 전반적인 아이디어는 웹 서비스에서 JSON을 가져 와서 함께 링크 한 다음 표시하는 사용자 지정 NSObject로 정렬하는 것입니다.내가 뭘 잘못하고 있니? NSFileManager woes
이것은 모두 잘되고 간다. 그러나 사용자가 인터넷 연결없이 장치에서이 정보를 볼 수 있기를 원하기 때문에 각 응용 프로그램에있는 Documents 폴더에 정보를 저장해야합니다.
기본적으로 문서 디렉토리의 하위 디렉토리에 저장하는 데 필요한 모든 사용자 지정 NSObject에 NSCoding 프로토콜을 구현했습니다.
이 모든 기능은 여기에서이 기능을 통해 수행됩니다. 사용자가 처음 응용 프로그램 (application didFinishLaunchingWithOptions:
) 때 사용자가 배경 (applicationWillEnterForeground:
)에있는 후 응용 프로그램을 엽니을로드 할 때
- (void)applicationDidEnterBackground:(UIApplication *)application
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0]; // Get documents folder
NSString *dataPath = [documentsDirectory stringByAppendingPathComponent:@"Data"];
NSString *dataFileString = [dataPath stringByAppendingPathComponent:@"Company.archive"];
if (![[NSFileManager defaultManager] fileExistsAtPath:dataFileString]) //Does directory already exist?
{
MACompany *company = [MACompany sharedMACompany];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0]; // Get documents folder
NSString *dataPath = [documentsDirectory stringByAppendingPathComponent:@"Data"];
NSError *error;
if (![[NSFileManager defaultManager] fileExistsAtPath:dataPath]) //Does directory already exist?
{
if (![[NSFileManager defaultManager] createDirectoryAtPath:dataPath
withIntermediateDirectories:NO
attributes:nil
error:&error])
{
NSLog(@"Create directory error: %@", error);
}
}
NSString *dataFileString = [dataPath stringByAppendingPathComponent:@"Company.archive"];
NSMutableData *data = [[NSMutableData alloc] init];
NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data];
[archiver encodeObject:company forKey:@"MACompany"];
[archiver finishEncoding];
[[NSFileManager defaultManager] createFileAtPath:dataFileString
contents:data
attributes:nil];
[archiver release];
[data release];
} else {
NSLog(@"File already exists, no need to recreate, not yet anyway");
}
}
나는 다음과 같은 요청을한다.
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0]; // Get documents folder
NSString *dataPath = [documentsDirectory stringByAppendingPathComponent:@"Data"];
NSString *dataFileString = [dataPath stringByAppendingPathComponent:@"Company.archive"];
if (![[NSFileManager defaultManager] fileExistsAtPath:dataFileString]) //Does directory already exist?
{
NSLog(@"Create a new Company Request");
MAWebRequests *companyReq = [[MAWebRequests alloc] init];
[companyReq getCompanyDetails];
[companyReq release];
} else {
NSLog(@"Saved Company Needs to be Decoded applicationWillEnterForeground");
MACompany *company = [MACompany sharedMACompany];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0]; // Get documents folder
NSString *dataPath = [documentsDirectory stringByAppendingPathComponent:@"Data"];
NSString *dataFileString = [dataPath stringByAppendingPathComponent:@"Company.archive"];
NSData *data = [[NSData alloc] initWithContentsOfFile:dataFileString];
NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:data];
[data release];
company = [unarchiver decodeObjectForKey:@"MACompany"];
[unarchiver finishDecoding];
[unarchiver release];
}
이제이 파일은 잘 작동하며이 파일에서도 가져올 수 있습니다. 그러나 Xcode의 디버거가 응용 프로그램에 연결되어있을 때만이 파일에 저장된 데이터를 가져올 수 있습니다. 중지 되 자마자 데이터가 손상되어 원래 데이터가 포함되지 않습니다.
나는 데이터를 저장하기 위해 위의 논리를 사용하지해야 ... 는 데이터는 여전히 저장되어, 내가 만든 파일을 볼 수 있지만 파일에 저장되는 실제 데이터 자체가 잘못된 것입니다 파일에 저장 한 다음 공유 객체를 다시 생성하십시오. 다른 사람이 그런 일을 시도하고 성공 했습니까? 이 이상한 문제가 나는 이유는 무엇입니까? 누구든지이 문제가 있습니까?
어떤 도움을 주시면 감사 드리며 모든 종류의 다양한 방법을 시도해 보았습니다. 아무런 효과가 없으며 아무 것도 얻을 수 없었습니다.
주요 감사 : 모든 나는
'[MAWebRequests alloc];은 좋지 않습니다. 이걸'초기화 '하는 걸 잊었 니? 또한, 시작 사이에 많은 양의 데이터를 유지해야 할 경우 CoreData 또는 SQLite를 사용하는 것이 좋습니다. – Yuji
init을 잊어 버렸습니다. 감사합니다! 그 비트를 청소했지만 여전히 내 문제를 해결하지는 않습니다 ... 저축하는 데이터의 크기를 추적했는데, 3kb는 전혀 아예 없습니다! :) –
좋습니다. 그에 맞게 게시물을 업데이트하십시오. 어쨌든 여기 붙여 넣은 코드는 괜찮아 보입니다. 사물을 인코딩하고 해독하는 코드를 게시해야합니다. 또한 Xcode (Build 메뉴 아래에 있음)에서 "Analyze"를 실행하고 코드를 수정하여 모든 경고를 제거해야합니다. – Yuji