Core Data NSFetchRequest problem - objective-c

I am trying to develop a test Core Data app so i can use it in my main app. But im having problems fetching stored data.
I have 1 Entity "User" with 2 attributes: name (String) & email (String)
I can successfully populate my store file with data...
here is the code im using below.
Any help would be greatly appreciated.
NSManagedObjectContext *managedObjectContext;
NSError *error;
NSString *basePath = #"/Users/Jamie/Desktop";
NSURL *storePath = [NSURL fileURLWithPath: [basePath stringByAppendingPathComponent:#"datastore.xml"]];
NSLog(#"Data Store path: %#", storePath);
NSPersistentStoreCoordinator *persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[NSManagedObjectModel mergedModelFromBundles:nil]];
if(![persistentStoreCoordinator addPersistentStoreWithType:NSXMLStoreType configuration:nil URL:storePath options:nil error:&error]) {
NSLog(#"Unable to load Core Data Store");
NSLog(#"ERROR: %#", error);
return 1;
} else {
NSLog(#"Loaded Core Data Store");
}
managedObjectContext = [[NSManagedObjectContext alloc] init];
[managedObjectContext setPersistentStoreCoordinator:persistentStoreCoordinator];
// Add data to store.
/*
NSManagedObject *mo = [NSEntityDescription insertNewObjectForEntityForName:#"User" inManagedObjectContext:managedObjectContext];
[mo setValue:#"jamie" forKey:#"name"];
[mo setValue:#"jamie#domain.com" forKey:#"email"];
if([managedObjectContext save:&error]) {
NSLog(#"Saved data to store");
} else {
NSLog(#"Unable to save data to store");
NSLog(#"ERROR: %#", error);
}
*/
// Fetch all the data - not worrying about using a predicate.
NSEntityDescription *entityDescription = [NSEntityDescription entityForName:#"User" inManagedObjectContext:managedObjectContext];
NSFetchRequest *request = [[NSFetchRequest alloc] init];
[request setEntity:entityDescription];
NSArray *results = [managedObjectContext executeFetchRequest:request error:&error];

Fixed - If you split out the parts into seperate methods it all works :)

Related

Updating only the first object in NSManagedObject

I want to update the first record in the NSManagedObject. What I have here updates all of them which I realise is because I am selecting them all and updating them all using the for but how do I just update the first record?
NSFetchRequest *fetchRequest = [NSFetchRequest fetchRequestWithEntityName:#"Users"];
NSError *errorLoading = nil;
self.users = [context executeFetchRequest:fetchRequestNext error:& errorLoading];
for (NSManagedObject *usersObject in [self users])
{
[usersObject setValue:#"*" forKey:#"currentUser"];
}
NSError *error;
[context save:&error3];
}
From the results (array) you simply select the one at index:0
NSArray *results = [context executeFetchRequest:fetchRequestNext error:& errorLoading];
if (results.count>0) {
NSManagedObject *userObject = results[0];
[usersObject setValue:#"*" forKey:#"currentUser"];
}
NSError *saveError = nil;
[context save:&saveError];
I hope this helps
Kind of like this:
NSFetchRequest *fetchRequest = [NSFetchRequest fetchRequestWithEntityName:#"Users"];
fetchRequest.fetchLimit = 1; //Fetch only one object (optional)
NSError *errorLoading = nil;
NSManagedObject *user = [[context executeFetchRequest:fetchRequestNext error:& errorLoading] firstObject];
if (errorLoading) {
//handle error
}
[user setValue:#"*" forKey:#"currentUser"];
NSError *error = nil;
if (![context save:&error]) {
//handle error
}
First of all, if you only need one object, you can set the fetchLimit to 1. It is not required, it's just a small optimization (it will make CoreData stop after fetching the first object). Then you execute the request just like you normally would and get the first object from the resulting array.
Another option is to use the method firstObject defined in NSArray.
NSFetchRequest *fetchRequest = [NSFetchRequest fetchRequestWithEntityName:#"Users"];
NSError *errorLoading = nil;
NSArray *users = [context executeFetchRequest:fetchRequestNext error:& errorLoading];
NSManagedObject *singleUser = [users firstObject];
if(singleUser){
[singleUser setValue:#"*" forKey:#"currentUser"];
}
NSError *error;
if([context save:&error]){
}

Decrypting core data

Warning: I don't know obj C but was handed this project for this one task
I am successfully encrypting the fields which lay in core data (sqlite) but I cannot decrypt them using a similar method. Below is my code which is causing the app to break during the fetch of core data:
NSManagedObjectContext *context = [self getContext];
NSEntityDescription *entityDesc = [NSEntityDescription entityForName:entityName inManagedObjectContext:context];
NSFetchRequest *request = [[NSFetchRequest alloc] init];
[request setEntity:entityDesc];
[request setReturnsObjectsAsFaults:NO];
if (predicate != nil) {
[request setPredicate:predicate];
}
NSError *error;
//the code that breaks
NSMutableDictionary *theFetch = [[context executeFetchRequest:request error:&error] mutableCopy];
return [[self decryptDictionaryData:theFetch withUserId:entityDesc.propertiesByName[#"theUnlockKey"]] mutableCopy];
decryptDictionaryData goes through each key in the dictionary and decrypts as follows:
decryptedResult = [RNDecryptor decryptData:[[NSData alloc] initWithBase64EncodedString:value options:0]
withSettings:kRNCryptorAES256Settings
password:key
error:&error];

Core data not working on device but fine on simulator

I am using core data for my application.It works fine on simulator but not retreiving the details on real device.Device is of iOS6.1.This is the code i am using:
- (NSManagedObjectContext *) getCurrentMangedContext
{
NSURL *modelURL = [[NSBundle mainBundle] URLForResource:#"ForceData" withExtension:#"momd"];
NSManagedObjectModel *managedObjectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL];
NSURL *storeURL = [[[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject]URLByAppendingPathComponent:#"ForceData.sqlite"];
NSError *error = nil;
NSPersistentStoreCoordinator *persistantStroreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:managedObjectModel];
if (![persistantStroreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:nil error:&error])
{
}
NSManagedObjectContext *managedContext = [[NSManagedObjectContext alloc] init] ;
[managedContext setPersistentStoreCoordinator:persistantStroreCoordinator];
modelURL = nil;
return managedContext;
}
This is how i am saving my login details and it is not giving any error.
NSEntityDescription *entity = [NSEntityDescription entityForName:#"User" inManagedObjectContext:context];
[request setEntity:entity];
NSManagedObject *newManagedObject = [NSEntityDescription insertNewObjectForEntityForName:[entity name] inManagedObjectContext:context];
if (emailString != nil)
{
[newManagedObject setValue:emailString forKey:#"email"];
}
if (genderString != nil)
{
[newManagedObject setValue:genderString forKey:#"gender"];
}
if (fNameString != nil)
{
[newManagedObject setValue:fNameString forKey:#"firstName"];
}
if (lNameString != nil)
{
[newManagedObject setValue:lNameString forKey:#"lastName"];
}
if (userIDString != nil)
{
[newManagedObject setValue:userIDString forKey:#"userID"];
}
NSError *error = nil;
if (![context save:&error])
{
NSLog(#"Unresolved error %#, %#", error, [error userInfo]);
abort();
}
// Insert User details to the user DB <--------------------------
And this is how i am retrieving:
- (User *) getActiveUser
{
NSManagedObjectContext *context = [self getCurrentMangedContext];
NSFetchRequest *request = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:#"User" inManagedObjectContext:context];
[request setEntity:entity];
NSError *errorFetch = nil;
NSArray *array = [context executeFetchRequest:request error:&errorFetch];
User *objUser = (User *) [array lastObject];
NSLog(#"%#",objUser);
return objUser;
}
But i am not getting the user details on device but getting on simulator.anyone faced this same?
In your case I'd suggest that ARC releases your managedObjectContext after executing the fetch request.
Make sure that you hold a strong reference to the appropriate managedObjectContext during the whole lifetime of your managedObject somewhere in your app (e.g. your ApplicationDelegate). A NSManagedObject can't live without it's managedObjectContext. The Core Data project template shows how to do that.
Further information about ARC and strong references: https://developer.apple.com/library/mac/releasenotes/ObjectiveC/RN-TransitioningToARC/Introduction/Introduction.html

NSFetchedResultsController, Core data and Threading

Hello fellow overflowers
I have a hard time figuring out how to fetch NSManagedObjects in a background thread and then show the results via a NSFetchedResultsController.
This is my code so far:
_theManagedObjectContext = [[DataManager sharedInstance] mainManagedObjectContext];
__block NSMutableArray *objectsIDs;
[[[DataManager sharedInstance] backgroundManagedObjectContext] performBlock:^{
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
fetchRequest.entity = [NSEntityDescription entityForName:#"Ret" inManagedObjectContext:[[DataManager sharedInstance] backgroundManagedObjectContext]];
NSArray *results = [[[DataManager sharedInstance] backgroundManagedObjectContext] executeFetchRequest:fetchRequest error:nil];
for (Ret *ret in results) {
NSManagedObjectID *moID = [ret objectID];
[objectsIDs addObject:moID];
NSLog(#"%#", objectsIDs);
}
[[[DataManager sharedInstance] mainManagedObjectContext ] performBlock:^{
[self loadDishesWithObjectIDs:objectsIDs];
}];
}];
First I fetch all the objects in a background thread and then transfering the NSMangedObjectIDs to the main thread.
In my "loadDishes" method:
- (void)loadDishesWithObjectIDs:(NSArray *)objectsIDs {
/*
[NSFetchedResultsController deleteCacheWithName:#"dishes"];
self.fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:**???**? managedObjectContext:_theManagedObjectContext sectionNameKeyPath:nil cacheName:#"dishes"];
_fetchedResultsController.delegate = self;
NSError *error = nil;
if (![_fetchedResultsController performFetch:&error]) {
NSLog(#"Fetch Failed");
}
NSArray *theDishes = _fetchedResultsController.fetchedObjects;*/
}
How would I manage to show the objects with NSFetchResultscontroller by the ObjectIDs which is fetched from the background thread ?
Thank you in advance :)
You would need to create a fetch request that looks something like this
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
fetchRequest.entity = [NSEntityDescription entityForName:#"Ret"
inManagedObjectContext:[[DataManager sharedInstance] mainManagedObjectContext]];
fetchRequest.predicate = [NSPredicate predicateWithFormat:#"self IN %#", objectIDs];

Recipes to update database in Core Data

I have a method for adding stamp to the card, from QR code reader to web service
my question is,
Here is my code:
- (void)addStamp
{
NSString *url = [_URL stringByAppendingString:#"add-stamp"];
NSString *post = [NSString stringWithFormat:#"userId=%#&code=%#", self.userId, self.code ];
post = [post stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSData *postData = [NSData dataWithBytes:[post UTF8String]
length:[post lengthOfBytesUsingEncoding:NSUTF8StringEncoding]];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL
URLWithString:url] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:600];
[request setHTTPMethod:#"POST"];
[request setHTTPBody:postData];
}
Rightnow I want to update my DB with new method -> if the code is repeated add stamp, if not create add a new card:
-(void)addStampInDB:(int)cardId
{
NSManagedObjectContext* managedObjectContext = [(AppDelegate*) [[UIApplication
sharedApplication] delegate] managedObjectContext];
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:#"Card"
inManagedObjectContext:managedObjectContext];
[fetchRequest setEntity:entity];
NSPredicate *predicate = [NSPredicate predicateWithFormat:#"remote_id = %d", cardId];
[fetchRequest setPredicate:predicate];
[managedObjectContext lock];
NSError *error;
NSArray *fetchedObjects = [managedObjectContext executeFetchRequest:fetchRequest
error:&error];
[managedObjectContext unlock];
Card *d = nil;
if([fetchedObjects count] > 0 ){
d = [fetchedObjects objectAtIndex:0];
cardId++;
} else{
d = [NSEntityDescription insertNewObjectForEntityForName:#"Card"
inManagedObjectContext:managedObjectContext];
}
NSArray *test;
for(NSDictionary *stamp in test)
{
d.remote_id = [NSNumber numberWithInt:[[stamp objectForKey:#"id"] intValue]];
d.stampNumber = [NSNumber numberWithInt:[[stamp objectForKey:#"stampNumber"] intValue]];
d.createdAt = [NSDate dateWithTimeIntervalSince1970:[[stamp objectForKey:#"createdAt"]
intValue]];
[managedObjectContext lock];
NSError *error;
if (![managedObjectContext save:&error])
{
NSLog(#"Whoops, couldn't save: %#", [error localizedDescription]);
NSLog(#"Failed to save to data store: %#", [error localizedDescription]);
NSArray* detailedErrors = [[error userInfo] objectForKey:NSDetailedErrorsKey];
if(detailedErrors != nil && [detailedErrors count] > 0) {
for(NSError* detailedError in detailedErrors) {
NSLog(#" DetailedError: %#", [detailedError userInfo]);
}
} else {
NSLog(#" %#", [error userInfo]);
}
}
[managedObjectContext unlock];
}
}
I don't know if I'm in a right way or not and also how I can test my method?
I'm not very sure about your question but I'll try to give some hints.
First
Card *d = nil;
if([fetchedObjects count] > 0 ){
d = [fetchedObjects objectAtIndex:0];
cardId++;
} else{
d = [NSEntityDescription insertNewObjectForEntityForName:#"Card"
inManagedObjectContext:managedObjectContext];
}
Here you have found an existing card, you use it, else you create a new. Question: what about cardId? Where is it used?
Second
for(NSDictionary *stamp in test)
{
d.remote_id = [NSNumber numberWithInt:[[stamp objectForKey:#"id"] intValue]];
d.stampNumber = [NSNumber numberWithInt:[[stamp objectForKey:#"stampNumber"] intValue]];
d.createdAt = [NSDate dateWithTimeIntervalSince1970:[[stamp objectForKey:#"createdAt"]
intValue]];
// other code here
}
If you loop test, you overwrite the value of your card d each time. Is it right?
Third
for(NSDictionary *stamp in test)
{
// other code here
}
// save here
Move the save out of the for loop. This boost performances in your app. It is not necessary to save each time in your loop. Do it at the end only.
Hope that helps.
P.S. If you want to know something else let me know.