• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Copying Managed Objects from App to Doc MOContext
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Copying Managed Objects from App to Doc MOContext


  • Subject: Copying Managed Objects from App to Doc MOContext
  • From: Jerry Krinock <email@hidden>
  • Date: Sat, 20 Dec 2008 15:58:52 -0800

My app maintains in its managed object context an array of, say, Potato objects. The potatoes in this central managed object context come and go occasionally -- someone might throw in a new one, or eat one. An archived potato is low in calories, consisting of a half dozen numbers or short strings.

My persistent documents can also contain several potatoes. To set one, user clicks a popup menu in the document window. The popup is populated by the potatoes currently available in the central managed object context.

Because the potatoes in this popup are inserted into the central managed object context, however, I cannot simply "set" one of them into my document when the user makes their seledtion. I implement a custom setter which inserts a ^new^ potato into the document's managed object context, and copies to it all of the attributes from the selected potato.

Looks OK to me, but having never seen anything like this, I was wondering if I'm seeing OK today.

Thank you,

Jerry Krinock


Here are a couple of (not yet fully tested) methods which can be implemented in a category or subclass of NSManagedObject to help with the all-attributes/value copying:



**************** .h *****************************

/*!
 @brief    A dictionary containing all attribute keys of the
 receiver for which current values are not nil, and their values.

 @details  Does not give relationships.  The output
 returned is mutable as a convenience because, in some
 applications, key/value pairs will be immediately
 added or removed.

 Tip: If you want to copy both attribues and properties, use
 -[NSKeyValueCodingProtocol dictionaryWithValuesForKeys:].
 */
- (NSMutableDictionary*)attributesDictionary ;

/*!
 @brief    Sets the receiver's attributes from a dictionary

 @details  All keys in 'attributes' are attempted
 to be set using setValue:forKey:.  Thus, setValue:forUndefinedKey:
 will be invoked (and possibly an exception raised) if 'attributes'
 contains keys for which the receiver is not KVC compliant.

 attributes The dictionary of attributes to be set.
 */
- (void)setAttributes:(NSDictionary*)attributes ;


**************** .m *****************************

- (NSMutableDictionary*)attributesDictionary {
NSEntityDescription* entity = [self entity] ;
NSDictionary* attributes = [entity attributesByName] ;
/* The following one-liner will almost work:
return [self dictionaryWithValuesForKeys:attributes] ;
Unfortunately, instead of omitting pairs for which the values are nil,
it inserts keys with values of NSNull. So, I use my own implementation:
*/


NSMutableDictionary* attributesDic = [[NSMutableDictionary alloc] init] ;
for (id attributeName in attributes) {
id value = [self valueForKey:attributeName] ;
if (value) {


            [attributesDic setValue:value
                             forKey:attributeName] ;
        }
    }

    return [attributesDic autorelease] ;
}

- (void)setAttributes:(NSDictionary*)attributes {
    for (id attributeName in attributes) {
        [self setValue:[attributes valueForKey:attributeName]
                forKey:attributeName] ;
    }
}


_______________________________________________

Cocoa-dev mailing list (email@hidden)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden


  • Follow-Ups:
    • Re: Copying Managed Objects from App to Doc MOContext
      • From: "Kyle Sluder" <email@hidden>
  • Prev by Date: Re: Newbie Question: implementing a stack
  • Next by Date: Re: selectionShouldChangeInTableView called twice
  • Previous by thread: Re: Find Panel Stopped Working (maybe)
  • Next by thread: Re: Copying Managed Objects from App to Doc MOContext
  • Index(es):
    • Date
    • Thread