Approach for handling a very large NSArray (and searching) on mobile
===

My app, which runs in a mobile (memory-constrained) environment, works with a large NSArray that acts as a dataset.  Each object in the dataset is an NSDictionary, and the size and content of each NSDictionary varies. The NSArray dataset is currently serialized in binary format, like so: 

     NSData *serArrData = [NSPropertyListSerialization dataFromPropertyList:myLargeArray
                                                               format:NSPropertyListBinaryFormat_v1_0
                                                     errorDescription:nil];  
     [serArrayData writeToFile:@"/Users/..."]; // Written somewhere, later imported into App.

In my program, I'm trying to peel through the NSArray (now a serialized NSData object, read from a file) without loading all of it into memory.


        streamData = [NSMutableData data];

        inputStream = [[NSInputStream alloc] initWithData:serArrData];
        [inputStream open]; 
         
        NSUInteger bytesR = 0; 
        while ([self.inputStream hasBytesAvailable]) 
        {
            uint8_t buf[2048];
            bytesR = [self.inputStream read:buf maxLength:2];
            [streamData appendBytes:(const void *)buf length:bytesR];

            //---Perhaps check [_data length] against array of lengths here---
        }

I'll be opening this NSInputStream when performing a search. I'm looking for a match between each NSDictionary object and some search term parameters. 

The myLargeArray dataset will not change. So, I was thinking I could record the byte size of each NSDictionary in the serialized myLargeArray, check [streamData length] against each of these recorded byte sizes, and then grab the NSDictionary via subdataWithRange:, or something. However, I'm not sure if this approach is even possible and/or feasible. My attempts thus far have of course been unsuccessful. 

Any guidance would be much appreciated. I can restructure the formatting of the dataset (an NSArray of NSDictionaries) differently, but still need to avoid loading it all into memory. Hence I'm using NSInputStream. Thanks. 
            
            