-(void)getRecords
{
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:#"CustomerOrder"
inManagedObjectContext:self.managedObjectContext];
[fetchRequest setEntity:entity];
NSError* error;
NSArray *fetchedRecords = [self.managedObjectContext executeFetchRequest:fetchRequest error:&error];
if (fetchedRecords)
{
if (fetchedRecords.count == 0) //create new Entity
{
NSLog(#"Balaiah babu");
}
else
{
int count =0;
for (int i=0; i<fetchedRecords.count; i++) {
CustomerOrder * record = [fetchedRecords objectAtIndex:i];
NSLog(#"%#,%#,%#,%#,%#,%#,%#,%#,%#,%#,%#",record.customer_id,record.order_id,record.outletLatitude,record.outletLongitude,record.trigger_days,record.trigger_end_date,record.trigger_from_time,record.trigger_no_days,record.trigger_radius,record.trigger_start_date,record.trigger_to_time);
NSLog(#"%d",count++);
}
}
}
}
here fetched Records array is showing nil.But it is going through loop and printing the values of the log.I don't know how it is happening.
In .h File
#property (strong, nonatomic) NSManagedObjectContext *managedObjectContext;
In .m File
Fetch entity "Info"
NSEntityDescription *entity =
[NSEntityDescription entityForName:#"Info"
inManagedObjectContext:managedObjectContext];
fetch entity request
NSFetchRequest *request = [[NSFetchRequest alloc] init];
[request setEntity:entity];
Fetch the records and handle an error
NSMutableArray *mutableFetchResults =
[[managedObjectContext executeFetchRequest:request
error:&error] mutableCopy];
if (!mutableFetchResults)
{
Nslog(#"nil");
}
Save our fetched data to an array
NSLog(#"get_Result==%#",[mutableFetchResults valueForKey:#"fname"]);
Related
Below is my code i am trying to fetch contain from coredata entity on privatequeue but in return i got null value.
-(NSArray *)fetch_agenda_for_financialdoc:(BOOL)isFinace
{
__block NSArray *fetchRecords;
NSManagedObjectContext *private = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSPrivateQueueConcurrencyType];
[private setParentContext:self.managedObjectContext];
[private performBlockAndWait:^{
NSFetchRequest *request = [[NSFetchRequest alloc]init];
NSEntityDescription *entity = [NSEntityDescription entityForName:#"AgendaDetails" inManagedObjectContext:private];
[request setEntity:entity];
NSError *error;
fetchRecords = [private executeFetchRequest:request error:&error];
}];
for (AgendaDetails *obj in fetchRecords) {
NSLog(#"%#",obj.agendaName);
}
return fetchRecords;
}
If more detail required i can give you thanks in advance.
How to convert dictionary data to NSManagedObject data?
I used propertiesToFetch, and it returns dictionary data. But I want NSManagedObject data.
please guide me
[fetchRequest setPropertiesToFetch:[NSArray arrayWithObjects:catalogID, pageID, nil]];
[fetch setResultType:NSDictionaryResultType];
[fetch setReturnsDistinctResults : YES];
NSError* error = nil;
self.resultPageArray = [context executeFetchRequest:fetch error:&error];
When I changed the type to NSManagedObjectResultType, then I got non-distinct data. I read that while using propertiesToFetch it saves the data in dictionary format. But I want to get the distinct data and save it into the NSManagedObjects
Try This,
[fetchRequest setPropertiesToFetch:[NSArray arrayWithObjects:catalogID, pageID, nil]];
[fetch setResultType:NSManagedObjectResultType];
[fetch setReturnsDistinctResults : YES];
NSError* error = nil;
self.resultPageArray = [context executeFetchRequest:fetch error:&error];
My code :
NSError *error = nil;
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:#"BookInfo"
inManagedObjectContext:self.managedObjectContext];
[fetchRequest setEntity:entity];
[fetchRequest setPropertiesToFetch:#[catalogID, pageID]];
[fetchRequest setPredicate: [NSPredicate predicateWithFormat:#"(bookId == %#)",strId]];
NSArray *fetchedItems = [self.managedObjectContext executeFetchRequest:fetchRequest error:&error];
if( fetchedItems.count > 0 ) {
BookInfo *bInfo = [fetchedItems lastObject];
NSLog(#"%#",bInfo.bookName);
}
I have solved the issue. I was fetching the data of NSManagedObjects. After that I saved that data to the other array without repetition. And return the new array.
self.resultPageArray = [context executeFetchRequest:fetch error:&error];
NSMutableArray *resultantFinalArr = [[NSMutableArray alloc] init];
for (int i = 0; i<resultPageArray.count; i++)
{
catalogPageDataModel *atIndexI = [resultPageArray objectAtIndex:i];
BOOL find = NO;
for (catalogPageDataModel* catalogPageDataModel in resultantFinalArr)
{
if (catalogPageDataModel.pageid.intValue == atIndexI.pageid.intValue)
{
find = YES;
break;
}
}
if (!find)
[resultantFinalArr addObject:atIndexI];
}
NSLog(#"result array count %lu",(unsigned long)resultantFinalArr.count);
NSLog (#"names: %#",resultantFinalArr);
return resultantFinalArr;
I've my entity in CoreData named as myentity and an attribute named as myuniqueid and I want to run an SQL query as follows:
Select * from myentity where myuniqueid == 1000
How can I get my specified rows where it matches as in SQL query? such that I want to filter all rows where myuniqueid is 1000.
My Code:
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSManagedObjectContext *context = [self managedObjectContext];
if (context == nil) {}
else {
NSEntityDescription *entity = [NSEntityDescription entityForName:#"myentity" inManagedObjectContext:context];
NSPredicate *predicate = [NSPredicate predicateWithFormat:#"myuniqueid == 1000"];
[fetchRequest setPredicate:predicate];
[fetchRequest setEntity:entity];
NSArray *items = [context executeFetchRequest:fetchRequest error:nil];
for (int i=0; i<4; i++) {
NSManagedObject *mo = [items objectAtIndex:i]; // assuming that array is not empty
id value = [mo valueForKey:#"myuniqueid"];
NSLog(#"SENDER: %#",value);
}
}
Im getting error: 'Unable to generate SQL for predicate (myuniqueid == 1000) (problem on RHS)'
Try this predicate :
NSFetchRequest *fetchRequest = [NSFetchRequest fetchRequestWithEntityName:#"myentity"];
fetchRequest.predicate = [NSPredicate predicateWithFormat:#"myuniqueid == %#", #(1000)];
NSArray *items = [[self managedObjectContext] executeFetchRequest:request error:nil];
While fetching data, it returns records with null values (number of records multiply by method call count)
NSManagedObjectContext *context = [self getContext];
NSFetchRequest *request = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:#"Category_List" inManagedObjectContext:context];
[request setEntity:entity];
NSError *error1 = nil;
NSArray *results1 = [context executeFetchRequest:request error:&error1];
NSLog(#"Fetch Request Data Count = %lu", (unsigned long)[results1 count]);
I just call the reset method of the context and my problem is solved.
[context reset];
I am having some difficulties in deleting an entry from Core Data.
It works but the NSManagedObject data has no values (fault), and so sometimes the wrong entry will be deleted.
In my deleteWholeDatabase method the objects are filled with values.
And in the deleteEntryFromDatabase method are no values. But why?
Here are my methods:
-(NSArray*)databaseRequest{
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:currentEntity inManagedObjectContext:managedObjectContext];
[fetchRequest setEntity:entity];
[fetchRequest setReturnsObjectsAsFaults:NO];
NSError *error;
NSArray *items = [managedObjectContext executeFetchRequest:fetchRequest error:&error];
return items;
}
-(void)deleteWholeDatabase{
for (NSManagedObject *objectToDelete in [self databaseRequest]) {
[managedObjectContext deleteObject:objectToDelete];
NSLog(#"%# object deleted",objectToDelete);
}
[self saveContext];
NSLog(#"All entries deleted!");
}
-(void)deleteEntryFromDatabase:(NSManagedObjectContext*)context forEntry:(NSIndexPath*)indexPath{
NSManagedObject *objectToDelete=[[self databaseRequest] objectAtIndex:indexPath.row];
[managedObjectContext deleteObject:[managedObjectContext objectWithID:[objectToDelete objectID]]];
[self saveContext];
NSLog(#"%# deleted!",[managedObjectContext objectWithID:[objectToDelete objectID]]);
}
The wrong entry is being deleted because each request return the itens in a different order. Try deleting the intens by comparing their memory adress
Instead of keeping a calling [self databaseRequest] everywhere, I would keep a local copy of NSManagedObjectID's as an array.
- (void)init {
self.entities = [self databaseRequest];
}
-(NSArray*)databaseRequest{
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:currentEntity inManagedObjectContext:managedObjectContext];
[fetchRequest setEntity:entity];
[fetchRequest setReturnsObjectsAsFaults:NO];
NSError *error;
NSArray *items = [managedObjectContext executeFetchRequest:fetchRequest error:&error];
NSMutableArray *itemIDs = [[NSMutableArray alloc] initWithCapacity:[items count]];
for (NSManangedObject *item in items) {
[itemIDs addObject:[item objectID]];
}
return items;
}
Then everywhere where you call [self databaseRequest], you will get an have the correct NSManagedObjectID. Then you just need to get the correct entity from the ID.