• 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
Using proxy objects with NSMutableArray
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Using proxy objects with NSMutableArray


  • Subject: Using proxy objects with NSMutableArray
  • From: Michael Tyson <email@hidden>
  • Date: Fri, 13 Jul 2007 16:50:42 +1000

Hi!

I'm archiving an NSMutableArray object to a file; the array contains some objects of a certain class. The objects in the array are actually stored elsewhere, within their own separate bundles, so I don't want them actually archived - I just want placeholders. So, I'm using a willEncodeObject delegate method to save a proxy object instead, which just contains the identifier of the object being archived. Then, I use a didDecodeObject delegate method to look up the real object, load it, and return it instead of the proxy

However, I'm getting some odd behaviour - I seem to be getting back not the mutable array, but the last object in the array.

The archiving process seems to be fine; if I archive a list with two FLFeed items, and I unarchive with the delegate disabled, I get back the mutable array with two FLFeedProxy items.
However, if I unarchive with the didDecodeObject delegate enabled, I just get a single FLFeed (the last one in the saved list).


Any ideas?

Thanks in advance!


Here're the relevant snippets:

---

- (BOOL)writeAllDataToDisk {
NSArray *libPaths = NSSearchPathForDirectoriesInDomains (NSLibraryDirectory, NSUserDomainMask, YES);
NSString *treePath = [NSString stringWithFormat:@"%@/Application Support/MyApplication/Tree.plist",
[libPaths objectAtIndex:0]];


NSMutableData *data = [[NSMutableData alloc] init];
NSKeyedArchiver *coder = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data];


// Set self as delegate so we can replace FLFeeds with a proxy object
[coder setDelegate:self];


    [coder encodeObject:list forKey:@"root"];
    [coder finishEncoding];

    [data writeToFile:treePath atomically:NO];
    [coder release];
    [data release];

    return YES;
}

- (void)readFromDisk {
NSArray *libPaths = NSSearchPathForDirectoriesInDomains (NSLibraryDirectory, NSUserDomainMask, YES);
NSString *treePath = [NSString stringWithFormat:@"%@/Application Support/MyApplication/Tree.plist",
[libPaths objectAtIndex:0]];


    NSData * data = [NSData dataWithContentsOfFile:treePath];

    if ( !data ) {
        return;
    }

NSKeyedUnarchiver *decoder = [[NSKeyedUnarchiver alloc] initForReadingWithData:data];

    // Set self as delegate to turn feed proxies into real feed objects
    [decoder setDelegate:self];

    id decoded = [decoder decodeObjectForKey:@"root"];
    [decoder finishDecoding];

    if ( decoded && [decoded isKindOfClass:[NSMutableArray class]] ) {
        list = [decoded retain];

// Remove all nil items from array (these represent feeds that could not be loaded)
int i;
for ( i=0; i<[decoded count]; i++ ) {
id obj = [decoded objectAtIndex:i];
if ( !obj ) {
[decoded removeObjectAtIndex:i--];
}
}
}


    [decoder release];
}

#pragma mark Archiving

- (id)archiver:(NSKeyedArchiver *)archiver willEncodeObject:(id)object {
// Encode a proxy for FLFeed objects (as FLFeeds are stored separately)
if ( [object isKindOfClass:[FLFeed class]] ) {
[object save];
return [[[FLFeedProxy alloc] initWithFeed:object] autorelease];
}
return object;
}


- (id)unarchiver:(NSKeyedUnarchiver *)unarchiver didDecodeObject:(id) object {
// Get real feed when decoded a feed proxy
if ( [object isKindOfClass:[FLFeedProxy class]] ) {
FLFeed * feed = [object feed];
[object release];


        if ( !feed ) {
            return nil;
        }

        if ( feed ) [feed retain];
        return feed;
    }
    return object;
}

---

--
Michael Tyson | michael.tyson.id.au

m: (+61) 0407 754 124
e: email@hidden
aim: mikerusselltyson

_______________________________________________

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: Using proxy objects with NSMutableArray
      • From: John Shockey <email@hidden>
  • Prev by Date: Re: Quick question: easy way to draw bitmap?
  • Next by Date: Re: Quick question: easy way to draw bitmap?
  • Previous by thread: Re: Quick question: easy way to draw bitmap?
  • Next by thread: Re: Using proxy objects with NSMutableArray
  • Index(es):
    • Date
    • Thread