I am very new to the iPhone development and although I spent hours in searching for the right answer I only got more confused at the end.
Here is my question - I'm making a simple iPhone application which has its own database/consisting of only one simple table and being stored in my projects folder along with all the .m and .h files/ and which uses the managed object model presented by core data.
I think I did everything the way it should be done but my program ends with the following "Unresolved error Error Domain=NSCocoaErrorDomain Code=256 "The operation couldn’t be completed. (Cocoa error 256.)
Actually here is the whole output:
2011-12-07 18:50:50.009 weatherForecast[5368:207] CoreData: error: (1) I/O error for
database at /Users/eln/Library/Application Support/iPhone
Simulator/5.0/Applications/4991C3EB-BDC2-4507-B1FB-720F6DC30245/Documents/weatherForecast.sqlite.
SQLite error code:1, 'no such table: Z_METADATA'
2011-12-07 18:50:50.012 weatherForecast[5368:207] Unresolved error
Error Domain=NSCocoaErrorDomain Code=256 "The operation couldn’t be
completed. (Cocoa error 256.)" UserInfo=0x6a33e40
{NSUnderlyingException=I/O error for database at
/Users/toma.popov/Library/Application Support/iPhone
Simulator/5.0/Applications/4991C3EB-BDC2-4507-B1FB-720F6DC30245/Documents/weatherForecast.sqlite.
SQLite error code:1, 'no such table: Z_METADATA',
NSSQLiteErrorDomain=1},
{
NSSQLiteErrorDomain = 1;
NSUnderlyingException = "I/O error for database at /Users/eln/Library/Application Support/iPhone
Simulator/5.0/Applications/4991C3EB-BDC2-4507-B1FB-720F6DC30245/Documents/weatherForecast.sqlite.
**SQLite error code:1, 'no such table: Z_METADATA'";
my program stops right in this get method:
**
- (NSPersistentStoreCoordinator *)persistentStoreCoordinator
{
if (__persistentStoreCoordinator != nil)
{
return __persistentStoreCoordinator;
}
NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:#"weatherForecast.sqlite"];
NSLog(#"DATABASE IS LOCATED AT: %#",(NSString *)[self applicationDocumentsDirectory]);
NSError *error = nil;
__persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];
if (![__persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:nil error:&error])
{
//right here my program aborts
NSLog(#"Unresolved error %#, %#", error, [error userInfo]);
abort();
}
**
I will be much grateful if someone gives me advise or at least suggestion on what might the problem be.Thank you in advance!
Core Data needs to manage/create its own database, it can't use one that you've created yourself. Is weatherForecast.sqlite such a file?
Related
I have a custom NSIncrementalStore. If there's a problem adding it, the error is automatically logged to the console.
The problem is that the options can contain sensitive data that I obviously don't want logged to the console.
I presume the error is logged by Core Data, which I don't really need, since I already have an NSError argument that I can use appropriately.
For example:
#implementation FakeStore
- (BOOL)loadMetadata:(NSError **)error {
*error = [NSError errorWithDomain:#"faildomain" code:531 userInfo:nil];
return NO;
}
#end
Attempt at adding the store:
[NSPersistentStoreCoordinator registerStoreClass:[FakeStore class] forStoreType:#"FakeStore"];
...
NSDictionary *options = #{#"option": #"sensitivedata"};
NSError *error;
[persistentCoordinator addPersistentStoreWithType:#"FakeStore" configuration:nil URL:storeUrl
options:options error:&error];
The error that is automatically logged to the console:
CoreData: error: -addPersistentStoreWithType:FakeStore configuration:(null) URL:<URL> options:{
option = sensitivedata;
} ... returned error Error Domain=faildomain Code=531 "The operation couldn’t be completed. (faildomain error 531.)" with userInfo dictionary {
}
A workaround is that the sensitive data shouldn't be passed into the options for my store, but it shouldn't be necessary.
Is there anyway to suppress this error?
well you can't turn it off but you should be able to redirect it. Bummer.. needs private API .. is that an option for you?
https://www.google.de/search?client=safari&rls=en&q=_NSSetLogCStringFunction&ie=UTF-8&oe=UTF-8&gfe_rd=cr&ei=uUKdVe-QN4nj8wfl9AE
tried the first link and found it still worked!
if private API is no option: no (and Id file a bug :D)
So I have a user drop some files into my application. Later I'm handling these files using NSFileHandle like this:
NSFileHandle *inputFile = [NSFileHandle fileHandleForReadingFromURL:path error:&error];
if(inputFile == nil) {
NSLog(#"Error (2): %#",error);
return;
}
On my Mac it works, but on other devices this error occurs:
Error Domain=NSCocoaErrorDomain Code=3 "The operation couldn't be completed. (Cocoa error 3.)" ...
I have looked into FoundationErrors.h but there is no error code 3 and i couldn't find anything useful using google.
What is causing this error (the description doesn't give much detail) and how can i prevent it?
I wrote some code to download a URL to my disk, using an NSURLRequest and an NSURLDownload. The download starts fine (at least, it sends downloadDidBegin:(NSURLDownload *)download), but then it sends
-(void)download:(NSURLDownload *)download didFailWithError:(NSError *)error
My implementation of the function is as follows:
{
[download release];
NSLog(#"Download failed! Error - %# %#",
[error localizedDescription],
[[error userInfo] objectForKey:NSURLErrorFailingURLStringErrorKey]);
}
The logged error is
2011-07-17 01:48:43.194 URLDownloader[28031:903] Download failed! Error - The operation couldn’t be completed. (NSURLErrorDomain error -3001.) [the URL in the NSURLRequest]
From what I can tell, this is the NSURLErrorCannotOpenFile error. I cannot find any explanation of why this error occurs, other then "Returned when NSURLDownload was unable to open the downloaded file on disk." from the documentation. So, why is this happening?
Fact that might be helpful(?): NSURLErrorCannotOpenFile is enumerated as kCFURLErrorCannotOpenFile. I have no idea what this means either....
Alright, so I was just being foolish. I had created a path using NSURL's absoluteString method instead of the path method. This was just bad coding on my part...
I've got a small utility application that parses a csv-file and uses the data to fill a Core Data store for use with another application. I open the csv-file using -initWithContentsOfFile:encoding:error: and the method always returns nil with the error
Error Domain=NSCocoaErrorDomain Code=260 UserInfo=0x100106450 "The file “data.csv” couldn’t be
opened because there is no such file." Underlying Error=(Error Domain=NSPOSIXErrorDomain Code=2 "The
operation couldn’t be completed. No such file or directory"), {
NSFilePath = "/Users/****/Library/Developer/Xcode/DerivedData/CreateData-
bhkmspyczgcfcrgehcbaydwdbfoa/Build/Products/Debug/data.csv";
NSUnderlyingError = "Error Domain=NSPOSIXErrorDomain Code=2 \"The operation couldn\U2019t be
completed. No such file or directory\"";
}
Now, I'm looking at that exact file in that exact directory. Even more confusing is the fact that I have another version of essentially the same utility for another app that works -- the only difference is the makeup of the core data store and entities and the number of columns in the csv file, all of which are invoked after loading the csv file. Or failing to load, as it were ...
int main (int argc, const char * argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
NSError *error = nil;
NSString *csvFile = [[NSString alloc] initWithContentsOfFile:csvFilePath(QUESTIONS_INPUT_FILENAME) encoding:NSASCIIStringEncoding error:&error]; // This fails
if (error != nil) {
NSLog(#"Returned error:\n%#, %#", error, [error userInfo]); // Returns the above error message
exit(1);
}
// ...
}
NSString *csvFilePath(NSString *inputFileName) {
NSString *resourcePath = [[NSBundle mainBundle] resourcePath];
return [resourcePath stringByAppendingPathComponent:inputFileName];
}
NSString *applicationDocumentsDirectory() {
return [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
}
It fails because you aren't passing in the actual location of the file, just the filename, so it is trying to look for it in the program's own folder - you're using the path of the bundle. unless that's where your csv file is, it won't find it.
To put it simply the file path you passed is incorrect thus can't be found or does not exist.
I was doing a rather ordinary addPersistentStore to an NSPersistentStoreCoordinator, and it generated an &error code.
So I went to NSLog it, and got an access error when I did this:
NSLog(#"Unresolved error %#, %#", error, [error userInfo]);
which seems to be the common idiom.
When I reformatted the error statement as follows:
NSLog(#"Unresolved error %#", [error userInfo]);
...the problem went away.
Even NSZombie didn't trap the bad access error correctly!
Any ideas?
How are you catching the error?
The correct way, as described by bbum, is:
NSError *error;
BOOL success = [blah blah:blah error:&error];
if (!success) {
NSLog(#"Error: %# %#", error, [error userInfo]); //Or other error handling (e.g., [NSApp presentError:error]).
} else {
//Succeeded—ignore the error variable entirely
}
(That's for a method blah:error:that returns a BOOL; the example in the question bbum answered was for a method that returned an object. The error-handling pattern is the same for both cases.)
According to his Twitter updates shortly afterward, some APIs will output an error object under the hood even if what you asked for succeeded, which means that testing the error variable instead of the BOOL return can fool you. I think this is what happened to you.
The solution is to only look at the error object if the API reports failure.
Do not forget to init NSError with nil value
NSError* err = nil;
[anObject doSomethingGetError:&err];
if (err) {
NSLog(...);
}
If that does not help, it is API bug