I'm in the process of adding core data to my iPad app. My general procedure is to test out code by putting it in the app delegate. When I'm satisfied I have the code parsed properly, I move it to the appropriate method elsewhere in the app. This has worked so far with adding objects. But I'm having issues with deleting objects. I can delete them fine from the app delegate by selecting them and deleting them, and right now I am deleting them all upon app start just to keep the database clean until I have the core data code working.
So enough of my workflow. The issue I'm having now is deleting an object from the appropriate spot. Let's see if I can briefly describe my setup.
I have two classes, let's call them Car
and Tire
for the sake of this discussion. I don't know what the convention is, so I came up with my own, namely to add C_
to the start of the name for the associated Core Data objects to store the objects associated with these objects. So I have two additional classes, C_Car
and C_Tire
. Each car can have several tires, so Car
has a property tires
, which is an NSSet
of Tire *
. Additionally Car
has a property called CarCD
of type C_Car *
, and Tire
has a property called TireCD
of type C_Tire *
so that each object can track its associated core data object (CD is for core data).
Xcode builds convenience methods to add or remove objects. Among these is the method to add a tire to a car. I use that thusly:
tire.tireCD = tireCD; //tire is an object of type Tire *, and tireCD is an object of type C_Tire *.
[self.carCD addTireObject:tireCD];//self.carCD is a property in my view controller of type CarCD *, and tireCD is a local object of type TireCD *.
Additionally, there is a convenience method to remove a tire from the car, - (void)removeTireObject:(C_Tire *)value;
. I figured that everywhere in my view controller where I had:
[tireObject removeFromSuperview];//tireObject is a (Tire *) object.
I would simply change that to:
[carObject removeTireObject:tireObject.tireCD];
[tireObject removeFromSuperview];
The first line should remove the object from the NSSet
on the core data object. The second line removes the object from the car object used in the app.
The problem is, I don't see any SQL being generated in the debugger window when this code is executed. Am I missing something?
Also, if you wish to comment on naming conventions, now is the time for me to adopt whatever is conventional.
(This is a summary from above comments plus some additional remarks.)
[car removeTireObject:tire]
just removes the tire from the car, it does not delete the tire object. To delete the object, call [context deleteObject:tire]
. For the naming conventions, see "Conventions" in "Programming with Objective-C". Some remarks:
CDCar
, CDTire
or, if you follow the suggestion to use a three letter prefix, XYZCar
, XYZTire
where XYZ
is some prefix chosen by you. CarView
, TireView
for UIView
subclasses.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.