Discrepancies between debug/release profiles when counting items from a plist for numberOfItemsInSection for UICollectionView

StackOverflow https://stackoverflow.com/questions/17096245

Вопрос

I have a UICollectionView whose collectionView:numberOfItemsInSection: is defined as follows:

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.contents.count;
}

self.contents is lazily allocated as follows:

- (NSArray *)contents
{
    if (!_contents) {
        _contents = [[XYZSharedMemoryStore sharedStore] clContents];
    }
    return _contents;
}

clContents returns an NSArray, as follows:

- (NSArray *)clContents
{
    NSString *path = [[NSBundle mainBundle] pathForResource:@"cl_contents" ofType:@"plist"];
    NSArray *products = [[NSArray alloc] initWithContentsOfFile:path];
    return products;
}

XYZSharedMemoryStore is a singleton defined as follows:

+ (id)sharedStore
{
    static XYZSharedMemoryStore *sharedStore = nil;
    if (!sharedStore) {
        sharedStore = [[super allocWithZone:NULL] init];
    }
    return sharedStore;
}


+ (id)allocWithZone:(NSZone *)zone
{
    return [self sharedStore];
}


- (id)init
{
    self = [super init];
    if (self) {
        // STUFF
    }
    return self;
}

Going full circle, the problem I'm having is that self.contents.count in collectionView:numberOfItemsInSection: returns 0 when I'm in a release profile and the correct number (10) in debug mode, so in the release profile, my UICollectionView doesn't display any cells. The profiles are in the default state that Xcode creates them in.

Any ideas what might be going on here?

Это было полезно?

Решение

So, this was a case of programmer error ...

I was setting my self.contents as a weak reference, so by all rights it should have been deallocated immediately and always returning a count of 0. Since the debug configuration has an optimization level of None [-O0], it wasn't deallocating immediately, allowing me to use it.

The release configuration is set to Fastest, Smallest [-Os], causing the weakly-referenced array to deallocate immediately, not allowing my UICollectionView to pick up its values.

Thanks to Artur Ozierański for his help.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top