I am working on Sqlite and I have added some questions and answers data. I am matching that data in a textfield. When I type the correct answer it's working fine, but what about approximately correct answers? I don't know how to do it.
Example: If I type CORRECT then if it matches, it works fine. But...
If I type CORRET, then it has to show "Answer is approximarely correct"
How to solve this? Can anyone help me. And I am matching Data from Sqlite.
Thanks & Regards.
Fetch the data from coredata with this code and compare with the textfield data as;
AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
NSManagedObjectContext *context = [appDelegate managedObjectContext];
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
// Edit the entity name as appropriate.
NSEntityDescription *entity = [NSEntityDescription entityForName:#"MileageTrack" inManagedObjectContext:context]; //Where MileageTrack is my coredata entity
[fetchRequest setEntity:entity];
NSMutableArray *array = [[NSMutableArray alloc] init];
int i=0;
NSError *error;
NSArray *objects = [context executeFetchRequest:fetchRequest error:&error];
for (MileageTrack *info1 in objects) {
NSMutableDictionary *dict1 = [[NSMutableDictionary alloc] init];
[dict1 setObject:info1.tripusage forKey:#"answer"];
if([[dict1 objectForKey:#"answer"] isEqualToString:textfield.text])
{
NSLog(#"correct answer");
}
else
{
NSLog(#"wrong answer");
}
This following sqlite command can help you.
[NSString stringWithFormat:"select username from tblData where name is %%#%",name];
this command will bring all your data,
like,
if you type "correct"
it will take 1correct, 2correct, anycorrect, correctany.
if it doesn't word try with %%;
Related
Im using a NSManagedObject as an attribute within my ViewController, declared like this:
#property(retain, nonatomic)NSManagedObject *person;
Im propagating the content of a fetch to a UITableView, when the user taps the contact he is looking for, this is what happens:
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
self.person = [contacts objectAtIndex:indexPath.row];
Contacts contains the result of the fetch, which is done like this:
NSArray *contactsArray;
NSError *error;
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSManagedObjectContext *context = [(OAuthStarterKitAppDelegate *)[[UIApplication sharedApplication] delegate] managedObjectContext];
NSEntityDescription *entity = [NSEntityDescription
entityForName:#"Contacts" inManagedObjectContext:context];
[fetchRequest setEntity:entity];
testForTrue = [NSPredicate predicateWithFormat:#"SOME CONDITION"];
[fetchRequest setPredicate:testForTrue];
[fetchRequest setFetchLimit:10];
contactsArray = [[NSArray alloc]initWithArray:[context executeFetchRequest:fetchRequest error:&error]];
When the user taps the contact, self.person has that value, but when I try to use that value in another method it's nil, and the address is 0x000000.
This only happens on iOS 5, on iOS 6 person has the value of the contact selected and I can use elsewhere.
Any ideas?
Thanks.
Instead of getting the NSManagedObject, which is always dependant of the life cycle of the context I just stored de NSManagedObjectID and then get the NSManagedObject by using the function objectWithID. And it worked!
I have Core Data setup in my app and need to fetch a bunch of items and then access the properties I choose of those fetched items. I am able to successfully fetch a bunch of results like this:
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:#"TableInfo" inManagedObjectContext:managedObjectContext];
[fetchRequest setEntity:entity];
NSError *error = nil;
NSArray *result = [managedObjectContext executeFetchRequest:fetchRequest error:&error];
This gives me an array with my results, great. Now from this how can I for example get the 'name' property from these results? In this specific circumstance I want to load an array with all of the fetched results 'name' strings.
If I read your question correctly, you are able to fetch your NSManagedObjects without difficulty, but would like to derive another NSArray with name properties on those managed objects?
Then you can use the valueForKeyPath on the NSArray (extending your original code):
NSArray *names = [result valueForKeyPath:#"name"];
You can use the key-value:
for (NSManagedObject *fetchedResult in result) {
NSLog(#"name = %#", [fetchedResult valueForKey:#"name"]);
}
or if you created your custom NSManagedObject:
for (EntityObject *fetchedResult in result) {
NSLog(#"name = %#", [fetchedResult name]);
}
I want to write generic method that checks if a given entity is in a Core Data database. I would like to have one method that works for all entities. I came up with something like this:
-(BOOL)checkIfExistsEntity:(NSString *)entityName withFieldName:(NSString *)fieldName andFieldValue:(NSString *)value{
NSManagedObjectContext *managedObjectContext = [(FGuideAppDelegate *) [[UIApplication sharedApplication] delegate] managedObjectContext];
NSEntityDescription *selectEntityDescription = [NSEntityDescription
entityForName:entityName inManagedObjectContext:managedObjectContext];
NSFetchRequest *fetchRequest = [[[NSFetchRequest alloc] init] autorelease];
[fetchRequest setEntity:selectEntityDescription];
NSPredicate *whereForFetch = [NSPredicate predicateWithFormat:#"%# = %#",fieldName, value];
[fetchRequest setPredicate:whereForFetch];
NSError *error = nil;
NSArray *array = [managedObjectContext executeFetchRequest:fetchRequest error:&error];
if (array != nil && [array count] > 0){
return YES;
}else {
return NO;
}
}
However it looks like the string #"%# = %#" in the predicate I wrote is not parsed properly. Is there any way to implement described functionality without hardcoding entities properties in a predicate?
Check out dynamic property names in the link below.
http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/Predicates/Articles/pCreating.html
Instead of %#, using %K should solve your problem
you need == not = (I think....)
You could use the countForFetchRequest method.
I have the following code, and I was wondering if theres any way to make this faster. Basically my app downloads some JSON (about 4000 records) from the net, and updates or creates my managed objects based on the data. At the moment it's quite slow, and I can see why, but I'm new to core data so I was wondering if there's anything I can do to make it faster?
NSFetchRequest *request = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:#"Company" inManagedObjectContext:managedObjectContext];
[request setEntity:entity];
NSMutableArray *coreDataArray = [[managedObjectContext executeFetchRequest:request error:nil] mutableCopy];
[request release];
for (NSDictionary *dict in arr) {
NSArray *filtered = [coreDataArray filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:#"(code == %#)", [dict objectForKey:#"Code"]]];
//NSLog(#"COREDATA ARRAY: %d FILTERED ARRAY: %d CODE: %# COREDATA FIRST CODE: %#", [coreDataArray count], [filtered count], [dict objectForKey:#"Code"], [[coreDataArray objectAtIndex:0] code]);
if ([filtered count] > 0) {
Company *c = [filtered objectAtIndex:0];
if ([dict objectForKey:#"Defunct"]) {
NSLog(#"DELETED DEFUNCT COMPANY");
[managedObjectContext deleteObject:c];
} else {
[c populateWithJSONDictionary:dict];
}
} else {
Company *c = (Company *)[NSEntityDescription insertNewObjectForEntityForName:#"Company" inManagedObjectContext:managedObjectContext];
[c populateWithJSONDictionary:dict];
}
float percent = (float)[arr indexOfObject:dict]/[arr count];
[self performSelectorInBackground:#selector(updateProgressView:) withObject:[NSString stringWithFormat:#"%f",percent]];
}
[coreDataArray release];
Many thanks for any help you can give.
You should check out the Core Data Programming Guide: Performance section
It has some specific advice for data import performance.
In case Apple moves the documentation again, here is a good search query on Google site:developer.apple.com core data import performance
I haven't seen any other questions quite like this on here, but I'm hoping someone has some insight. I'm just starting to learn Core Data.
Basically, I have two methods and I want to choose which one to call with an if/else statement based on whether or not the "Contacts" table contains any records. Is there a way using core data to check if there are any records in a table?
The best way I've found so far is to set the fetchLimit to 1 and then check to see if anything returns.
[request setFetchLimit:1];
But I keep thinking there has to be a better/easier way. Anyone know or have a good reference I can look at?
Thanks a ton!
Yes, definitely there is a better method. Setup a fetch request as usual, but, instead of actually executing it, simply ask for the number of objects it would have returned if it had been passed to executeFetchRequest:error:
This can be done using
- (NSUInteger)countForFetchRequest:(NSFetchRequest *)request error:(NSError **)error;
Something like this:
- (int) numberOfContacts{
NSFetchRequest *request = [[NSFetchRequest alloc] init];
NSManagedObjectContext *managedObjectContext = yourManagedObjectContext;
NSEntityDescription *entity = [NSEntityDescription entityForName:#"Contacts" inManagedObjectContext:managedObjectContext];
[request setEntity:entity];
NSError *error = nil;
NSUInteger count = [managedObjectContext countForFetchRequest:request error:&error];
[request release];
if (!error){
return count;
}
else
return -1;
}
It's not necessarily any better or easier, but you can look for a specific record and then create it if it doesn't exist like this:
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:#"Contact"
inManagedObjectContext:[self managedObjectContext]];
[fetchRequest setEntity:entity];
NSError *error;
// Filter based on a predicate
[fetchRequest setPredicate:
[NSPredicate predicateWithFormat:#"identifier == %#", #"1"]];
NSManagedObject *contact = [[managedObjectContext
executeFetchRequest:fetchRequest error:&error] lastObject];
// If the contact was not found
if (!contact)
{
// Create the contact
contact = [NSEntityDescription insertNewObjectForEntityForName:#"Contact"
inManagedObjectContext:managedObjectContext];
[contact setValue:[NSNumber numberWithInt:1] forKey:#"identifier"];
[managedObjectContext save:nil];
}
Marcus Zarra posted some code that demonstrates this in a feed reader app. Marcus is the Core Data master.