Using a Singleton Synchronous Array with NSThread

I have a book application with UISearchBar where the user enters the name of any book and gets the search results (from an external API call) below when it prints.

I use a singleton variable in my application called retrievedArray, which stores all the books.

@interface Shared : NSObject {
    NSMutableArray *books;
}

@property (nonatomic, retain) NSMutableArray *books;

+ (id)sharedManager;

@end

They are accessed in several .m files using NSMutableArray * retrievedArray; ... in the header file

retrievedArray = [[Shared sharedManager] books];

My question is how to ensure that the values ​​inside retrievedArray are stored in all classes.

In fact, the values ​​inside the retrievedArray are added via NSXMLParser (i.e. via the external web service API). There is a separate XMLParser.m file where I do all the parsing and populate the array. Parsing is performed in a separate thread.

    - (void) run: (id) param  {
        NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

        NSXMLParser *parser = [[NSXMLParser alloc] initWithContentsOfURL: [self URL]];
        [parser setDelegate: self];
    [parser parse];
        [parser release];

        NSString *tmpURLStr = [[self URL]absoluteString];

        NSRange range_srch_book = [tmpURLStr rangeOfString:@"v1/books"];

        if (range_srch_book.location != NSNotFound) 
            [delegate performSelectorOnMainThread:@selector(parseDidComplete_srch_book) withObject:nil waitUntilDone:YES];

        [pool release];
    } 


    - (void) parseXMLFile: (NSURL *) url
    {   
        NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
        [self setURL: url];
        NSThread* myThread = [[NSThread alloc] initWithTarget:self
                                                     selector:@selector(run:)


object: nil];
    [retrievedArray removeAllObjects];
    [myThread start];
    [pool release];
}

, , (, , ). , 2 , ; . A "", B ... .

, , .

: , 3 , , Item1, Item2 Item3, Item2, Item3 (.. ).

, ;

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    // Navigation logic -- create and push a new view controller

    if(bookdetailCustom == nil)
        bookdetailCustom = [[BookDetailCustom alloc] initWithNibName:@"BookDetailCustom" bundle:[NSBundle mainBundle]];

    //aBook = [retrievedArray objectAtIndex:indexPath.row];

    bookdetailCustom.selectedIndex = indexPath.row;

    [self.navigationController pushViewController:bookdetailCustom animated:YES];
    [bookdetailCustom release];
    bookdetailCustom = nil;
}

searchTabkleView

- (void) searchTableView {
    NSString *searchText = searchBar.text;
    NSMutableArray *searchArray = [[NSMutableArray alloc] init];

    for (int i=0;i<[retrievedArray count];i++)
    {
        Stock *aBookTemp = [retrievedArray objectAtIndex:i];
        NSString *temp = [aBookTemp valueForKey:@"BookName"];
        [searchArray addObject:temp];
    }

    for (NSString *sTemp in searchArray)
    {
        NSRange titleResultsRange = [sTemp rangeOfString:searchText options:NSCaseInsensitiveSearch];

        if (titleResultsRange.length > 0)
            [copyListOfItems addObject:sTemp];
    }

    [searchArray release];
    searchArray = nil;
}

, .

+5
5

, , retrievedArray NSMutableArray. , , .

NSMutableArray ; , , . , , . , , .

, :

  • "" , A N.
  • -. XML . .
  • N "". Detail N.
  • N , - , 2, N B. " " .
  • - XML , .

, - , 4 NSRangeException.

, . , List Detail , , . , , , ; , , , .

, , .

+5

nonatomic . Atomic - ( atomic, nonatomic ), , @synchronize.

, nonatomic , . , / Apple - , UI, - , UIKit .

, - - . - , , , -, - ( 100% ).

+2

, , Anomie . , , .

, . , , NSFetchedResultsController. , .

, , .

+1

NSRecursiveLock .

. NSRecursiveLock. :

NSRecursiveLock , , , , , . , , .

CoreVideo .

0

, retrievedArray . retrievedArray XML .

:

  • Modify parseXMLFile:to create a new array:parsedArray = [NSMutableArray array]
  • Modify parser:didEndElement:to add to this new array:[parsedArray addObject:aBook]
  • In parser:didEndDocument:pass the new array to the main thread:

    [delegate performSelectorOnMainThread: @selector(updateRetrievedArray:)
                               withObject: parsedArray
                            waitUntilDone: NO];
    
  • updateRetrievedArray:executed in the main thread will be the code responsible for updating retrievedArray- thus, only one thread modifies this object:

    - (void) updateRetrievedArray: (NSArray *)parsedArray {
        [retrievedArray setArray:parsedArray];
        [self parseDidComplete_srch_book]; // Be sure to call [tableView reloadData]
    }
    
0
source

All Articles