Problems with [request setFetchBatchSize:[self defaultBatchSize]]; #4

Closed
enricodetoma opened this Issue Feb 6, 2011 · 4 comments

Projects

None yet

2 participants

@enricodetoma

I can't absolutely understand why, but this line in NSManagedObjectContext+ActiveRecord.m:
[request setFetchBatchSize:[self defaultBatchSize]];
makes my app crash on the first query.

I investigated and noticed that [self defaultBatchSize], when called for the first time, sets a static NSNumber to a default value of kActiveRecordDefaultBatchSize and this default value can't be changed, because both setDefaultBatchSize and defaultBatchSize are private members.

So I simply removed setDefaultBatchSize and defaultBatchSize methods and replaced the crashing line with:
[request setFetchBatchSize:kActiveRecordDefaultBatchSize];
and now it doesn't crash any more.

@enricodetoma

Now I understand the problem. When doing this:

+ (void) setDefaultBatchSize:(NSUInteger)newBatchSize
{
    @synchronized(defaultBatchSize)
    {
        defaultBatchSize = [NSNumber numberWithInt:newBatchSize];
    }
}

you set the static variabile defaultBatchSize to an autoreleased object, which will probabily be destroyed.

You could probably use this, instead:

static NSInteger defaultBatchSize = -1;

@implementation NSManagedObject (ActiveRecord)

+ (void) setDefaultBatchSize:(NSInteger)newBatchSize
{
    defaultBatchSize = newBatchSize;
}

+ (NSInteger) defaultBatchSize
{
    if (defaultBatchSize < 0)
    {
        [self setDefaultBatchSize:kActiveRecordDefaultBatchSize];
    }
    return defaultBatchSize;
}

then you can also expose setDefaultBatchSize publicly to allow the user to change the default batch size, if he wants.

@casademora
Member

yeah, I hadn't had problems with it...but that sounds like a better solution. I'll find some time and make an update soon...thanks!

@enricodetoma

thank you for the update
one small problem remains:

+ (NSUInteger) defaultBatchSize
{
    if (defaultBatchSize == nil)
    {
        [self setDefaultBatchSize:kActiveRecordDefaultBatchSize];
    }
    return defaultBatchSize;
}

if (defaultBatchSize == nil) gives an error because of comparison between number and pointer
I simply suggest to leave:

+ (NSUInteger) defaultBatchSize
{
    return defaultBatchSize;
}
@casademora
Member

that's what I get for doing this quickly...I'll take care of it shortly...thanks!

This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment