is it valid statement? - ios4

SqlAssignementAppDelegate *appDelegate =(SqlAssignementAppDelegate *) [[UIApplication sharedApplication]delegate];
lblresNam.text =[appDelegate.Record objectAtIndex:0];
lblresNam is UITextField type object
Record is NSMutableArray
but my application is terminating..
Please give me solutions...

Is your Record object a property, so you know it's being retained, and so dot notation will be available?
More likely the culprit, did you forget to #synthesize Record so it has a getter?
If you neglected to do either one of those, appDelegate.Record is an error.

Related

PushViewController not working

i want to create a basic push view scenario. I have the following code but it does not work. Nothing happens. Could someone tell me why?
testController *screen2 = [[testController alloc] initWithNibName:nil bundle:nil];
[self.navigationController pushViewController:screen2 animated:YES];
[screen2 release];
You should try to do an NSLog to see if screen2 is nil. Chances are it is not being appropriately loaded from a corresponding nib for some reason. As an aside, I'd highly recommend sticking to the convention of capitalizing class names. Speaking of which, did you maybe call the nib file TestController.nib? (That would cause the problem.)

What's the point of -primitiveValueForKey:?

-setPrimitiveValue:forKey: won't trigger KVO notifications. But in my brain, KVO only makes sense when something changes. But how can change something when I only access it for read?
-primitiveValueForKey: only gets the object for some key. But it won't modify it. So why would/could this cause KVO notifications when using -valueForKey:?
(Of course there is a point, but I don't see it yet.)
The -primitiveValueForKey: and -setPrimitiveValue:forKey: methods are primarily used by Core Data. In particular, Core Data doesn't just need to know when you're going to modify an attribute; it needs to know when you're going to access it as well, so it can implement faulting.
Thus Core Data adds -{will,did}AccessValueForKey: methods to use in getters, just as -{will,did}ChangeValueForKey: methods exist for use in setters to act as KVO hooks.
However, there's another wrinkle: Core Data actually manages the underlying storage of modeled properties for you as well. So you need some way to manipulate this underlying storage within the barriers established by the -{will,did}{Access,Change}ValueForKey: methods. That's where -primitiveValueForKey: and -setPrimitiveValue:forKey: come in.
This is why the standard pattern for implementing Core Data getters and setters, prior to the existence of #property and #dynamic, looked like this:
// Person.m
#import "Person.h"
#implementation Person
- (NSString *)name {
[self willAccessValueForKey:#"name"];
NSString *value = [self primitiveValueForKey:#"name"];
[self didAccessValueForKey:#"name"];
}
- (void)setName:(NSString *)value {
[self willChangeValueForKey:#"name"];
[self setPrimitiveValue:value forKey:#"name"];
[self didChangeValueForKey:#"name"];
}
#end
Now of course, you can just declare a property and define it as #dynamic if you want Core Data to generate this stuff for you at runtime:
// Person.h
#interface Person : NSManagedObject
#property (nonatomic, readwrite, copy) NSString *name;
#end
// Person.m
#import "Person.h"
#implementation Person
#dynamic name;
#end
There are some situations though where you still want to manipulate the underlying storage without KVO or fault-firing, however. Thus Core Data provides a new way to get at this as well, also built around property declarations and automatic synthesis:
// Person.h
#interface Person : NSManagedObject
#property (nonatomic, readwrite, copy) NSString *name;
#end
// Person.m
#import "Person.h"
#interface Person ()
#property (nonatomic, readwrite, copy) NSString *primitiveName;
#end
#implementation Person
#dynamic name;
#dynamic primitiveName;
#end
Note that I put the class continuation in the .m file; it's not something that code outside Person (or even really code outside -awakeFromInsert and -awakeFromFetch) should touch. But it does let me get at the underlying storage for the "name" property without embedding literal strings in my code, and with the use of the real types.

Seemingly inconsistent behavior among NSManagedObject subclasses

I have several subclasses of NSManagedObject. They are all instantiated with code something like this:
MeasurementDescriptor *descriptor = (MeasurementDescriptor *)[NSEntityDescription
insertNewObjectForEntityForName:#"MeasurementDescriptor"
inManagedObjectContext:context];
or like this:
Experiment *experiment = (Experiment *)[NSEntityDescription
insertNewObjectForEntityForName:#"Experiment"
inManagedObjectContext:context];
What is odd, though, is that (from code above)
NSLog(#" descriptor's class = %#", NSStringFromClass([descriptor class]));
prints out 'NSManagedObject', while
NSLog(#" experiment's class = %#", NSStringFromClass([experiment class]));
prints out 'Experiment'.
Does anyone know why this would be? MeasurementDescriptor, unlike my other NSManagedObject subclasses, had no ivars (not including its Core Data properties). Adding an ivar did not change anything. Similarly, MeasurementDescriptor is the only NSManagedObject subclass without 'relationship' properties. Perhaps this accounts for this strangeness...???
The only explaination is that your MeasurementDescriptor subclass is not actually known to the code. The most common causes of this are:
In the data model editor not setting the Class attribute of the entity to the correct class.
Not adding the source file for the subclass to the target.
This is easy to do with Core Data because if it can't find a dedicated subclass it doesn't complain but just returns a generic NSManagedObject initialized with the entity's property key names.

NSPredicate cause update editing to return NSFetchedResultsChangeDelete not NSFetchedResultsChangeUpdate

I have predicate inside of - (NSFetchedResultsController *)fetchedResultsController in a standard way starting from the CoreDataBook example.
NSPredicate *predicate = [NSPredicate predicateWithFormat:#"state=%# && date >= %# && date < %#", #"1",fromDate,toDate];
[fetchRequest setPredicate:predicate];
This works fine however when editing an item, it returns with NSFetchedResultsChangeDelete not Update. When the main view returns, it is missing the item. If I restart the simulator the delete was not saved and the correct editing result is shown the the predicate working correctly.
case NSFetchedResultsChangeDelete:
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
break;
I can confirm the behavior by commenting out the two predicate lines ONLY and then all works as it should correctly returning with the full set after editing and calling NSFetchedResultsChangeUpdate instead of NSFetchedResultsChangeDelete.
I have read http://matteocaldari.it/2009/11/multiple-contexts-controllers-delegates-and-coredata-bug
who reports similar behavior but I have not found a work around to my problem.
I meet this problem too. The root reason is caused by setting the value of attribute in the NSPredicate format string with a wrong type.
For this problem may be the attribute of "state" has a Number type, like "Integer 32". But the code above passed a NSString to it(#"1"), just change it to a NSNumber(#1), will fixed the problem.
Matthew Weiss finally fixed the problem by using fetch request template. I think the "state" is checked in the template which doesn't have the type problem. And only fromDate and toDate are passed by subs.
Sorry it has been awhile and I am soon to release the product. The work around was to make stored procedures in the fetched object and use a
NSFetchRequest *fetch = [model fetchRequestFromTemplateWithName:#"myTemplate1" substitutionVariables:subs];
This eliminates the use of Predicate on the FetchedResultsController.
When I want to use predicate I just make sure that I am using a local managedObjectContext and pass the Array itself to work on elsewhere. NSArray *array = [moc executeFetchRequest:fetchRequest error:&error]; return array;
This is a safer bet and allows you to make many view of the same data.
Are you using more than one NSManagedObjectContext? The post you linked to was using multiple NSManagedObjectContext instances improperly which is the root of his problem. It is not a bug in the NSFetchedResultsController but a misunderstanding on his part as to how it works.
With regard to your issue; first question: do you have a break in the case before the one you showed?
What does the editing code look like? You show a predicate but not the code that is editing the object that you state is causing the NSFetchedResultsChangeDelete to fire.
In my post I'm dealing with a known bug in NSFetchedResultsController (see https://devforums.apple.com/message/139580#139580) which does a pointer comparison between two objects in two different managed object context, instead evaluating objectID, so I think this is not your problem.
Is your object treated as deleted just if you modify its date outside the interval specified in the NSPredicate or for any other modification as well?

insert and modify a record in an entity using Core Data

I tried to find the answer of my question on the internet, but I could not.
I have a simple entity in Core data that has a Value attribute (that is integer) and a Date attribute. I want to define two methods in my .m file.
First method is the ADD method. It takes two arguments: an integer value (entered by user in UI) and a date (current date by default). and then insert a record into the entity based on the arguments.
Second method is like an increment method. It uses the Date as a key to find a record and then increment the integer value of that record.
I don't know how to write these methods. (assume that we have an Array Controller for the table in the xib file)
The second method you are looking for is not appropriate to add to the entity itself. It needs to be somewhere above the entities, most likely in your controller object.
The first method is as follows:
- (void)updateDate:(NSDate*)date andValue:(NSInteger)value
{
[self setValue:date forKey:#"date"];
[self setValue:[NSNumber numberWithInteger:value] forKey:#"value"];
}
This is fairly straight KVC (Key-Value Coding) and I highly recommend that you read Apple's docs on the subject.
For your other method, that should be in a controller, you need to perform a fetch to find the record.
- (id)findRecordForDate:(NSDate*)date inManagedObjectContext:(NSManagedObjectContext*)moc
{
NSFetchRequest *request = [[NSFetchRequest alloc] init];
[request setEntity:[NSEntityDescription entityForName:#"MyEntity" inManagedObjectContext:moc]];
[request setPredicate:[NSPredicate predicateWithFormat:#"date == %#", date]];
NSError *error = nil;
NSArray *objects = [moc executeFetchRequest:request error:&error];
NSAssert1(error == nil, #"Error fetching object: %#", [error localizedDescription]);
return [objects lastObject];
}
- (void)incrementEntityWithDate:(NSDate*)date
{
id entity = [self findRecordForDate:date inManagedObjectContext:[self managedObjectContext]];
NSInteger value = [[entity valueForKey:#"value"] integerValue];
value += 1;
[entity setValue:[NSNumber numberWithInteger:value] forKey:#"value"];
}
This is also very straightforward Core Data access. I recommend you read up on how Core Data works.
As an aside, using a date as a unique is a very bad design.
UPDATE
Marcus, Thanks for the answer. It is really helpful. I am new to core data so I have a few questions to make things clearer.
The code for the first method sets two values, but it doesn't insert the new record into the table. How can I insert the newly created record into the table?
You need to read up on how Core Data works. Apple has great documentation on how Core Data works and if that fails you can buy my book. There is a TON of information about how to use Core Data on the internet.
Where should I put the first method? in my .m file?
This is basic Objective-C. If you are asking this question you need to step way back and learn the fundamentals of the language first.
You mentioned that I need to add the second method in the controller. But the controller is defined in the xib file. How can I add the second method to that?
The controller is never defined in the xib file. It is referenced in the xib file. Again, you need to go back to the beginning of how Objective-C works and learn the basics before you dive this deep.

Resources