• 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
Re: Objects cross calls
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Objects cross calls


  • Subject: Re: Objects cross calls
  • From: Lorenzo <email@hidden>
  • Date: Mon, 23 Jan 2006 19:17:25 +0100

My fruit is an object that can contain other fruits in an array called
children, like a hierarchical table.
So when I release a fruit, in the dealloc method I have

- (void)dealloc
{
    if(children){
        [children release];
        children  = nil;
    }

    [super dealloc];
}

Think that now I delete a fruit with

- (void)deleteFruit:(int)index
{
    aFruit = [fruits objectAtIndex:index];
    [[aFruit basket] removeFruit:aFruit];
    [fruits removeObjectAtIndex:index];
}

this cannot work because the fruit within the array fruits could contain
other fruits which can be retained by other groups. It's complicated. I
would find a way to disconnect any relationship between groups and fruits
just when I release the fruit or the group. But I cannot figure out how.
A developer told me to use a forward declaration, review @class. I have
taken a look there, but no way to understand how this solution can be
implemented here.


Best Regards
--
Lorenzo
email: email@hidden
>
> From: Mont Rothstein <email@hidden>
> Date: Mon, 23 Jan 2006 08:01:24 -0800
> To: Lorenzo <email@hidden>
> Cc: Pontus Ilbring <email@hidden>, email@hidden
> Subject: Re: Objects cross calls
>

> First, just so you know, this problem (where two objects retain each other) is
> generally referred to as a retain cycle.
>
> You are on the right track with deleteFruit:
>
> You always want to wrap you instance variables (like fruit) in accessor
> methods.  No code, other than the accessor methods, should ever directly
> access instance variables.  (I use an underscore in front of instance
> variables to help remind myself not to access them directly, though I think
> Apple may discourage this for some silly reason).
>
> So, if you want to delete all fruit then create a deleteAllFruit method and a
> removeFruits: method on the basket.  You can then deal with your retain cycles
> without having to worry about some other random bit of code screwing things up
> for you.
>
> I'm sure there is a name for the accessor method pattern but someone more
> learned that I would have to tell you what it is.
>
> -Mont
>
>
> On 1/23/06, Lorenzo <email@hidden> wrote:
>> The problem is that I cannot delete the fruit from the basket in the fruit
>> dealloc method this way
>>
>> - dealloc
>> {
>>     [basket removeFruit:self];
>>     [super dealloc];
>> }
>>
>> in fact when some part of my app deletes a fruit, e.g.
>>
>>     [fruits removeObjectAtIndex:4];
>>
>> since the retain count was 2 before the removeObjectAtIndex call,
>> the dealloc method doesn't get called.
>> I should remove a fruit with my own method like
>>
>> - (void)deleteFruit:(int)index
>> {
>>     aFruit = [fruits objectAtIndex:index];
>>     [[aFruit basket] removeFruit:aFruit];
>>     [fruits removeObjectAtIndex:index];
>> }
>>
>> This begins to be complicated if you think that some call could remove all
>> the fruits or even release the fruits array like these calls
>>
>>     [fruits  removeAllObjects];
>>     [fruits release];
>>
>> What to do in case of these 2 calls above?
>> I think there should be a more efficient method than cross-connecting the 2
>> objects. I though about relational database and record IDs but I am afraid
>> that is not the right approach.
>> What's about creating an array with simple pointers of the fruits?
>>
>>
>>
>> Best Regards
>> --
>> Lorenzo
>> email: email@hidden
>>
>>> > From: Pontus Ilbring <email@hidden>
>>> > Date: Mon, 23 Jan 2006 14:51:33 +0100
>>> > To: Lorenzo <email@hidden>
>>> > Cc: email@hidden
>>> > Subject: Re: Objects cross calls
>>> >
>>> > On 1/23/06, Lorenzo <email@hidden> wrote:
>>>> >> Hi,
>>>> >> how should I work with objects referring each other in a cross way?
>>> >
>>> > Very, very carefully, that's how. You seem to have figured that out
>>> already.
>>> >
>>> > For your situation I would suggest a very simple solution. When you
>>> > remove an object from the fruits array, tell -all- baskets to remove
>>> > it as well. There's no harm in trying to remove an object from an
>>> > array which doesn't contain it.
>>> >
>>> > If you don't need them ordered then you should also consider changing
>>> > the baskets to use sets instead of arrays, as they are much more
>>> > efficient when you want to remove arbitrary objects.
>>
>>  _______________________________________________
>> Do not post admin requests to the list. They will be ignored.
>> Cocoa-dev mailing list      (email@hidden
>> <mailto:email@hidden> )
>> Help/Unsubscribe/Update your Subscription:
>>
>> This email sent to email@hidden
>
>


 _______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:

This email sent to email@hidden

  • Follow-Ups:
    • Re: Objects cross calls
      • From: Camillo Lugaresi <email@hidden>
References: 
 >Re: Objects cross calls (From: Mont Rothstein <email@hidden>)

  • Prev by Date: Re: Database frameworks for Cocoa
  • Next by Date: Re: Using NSAppleEventDescriptor with NSDates
  • Previous by thread: Re: Objects cross calls
  • Next by thread: Re: Objects cross calls
  • Index(es):
    • Date
    • Thread