This is my code for loading data from sqlite file

- (void) loadInitialData{

    // Form the query.
    NSString *query = @"select * from tasklist";

    // Get the results.
    if (self.toDoItems != nil) {
        self.toDoItems = nil;
    }


    NSArray *dbResultArray = [[NSArray alloc] initWithArray:[self.dbManager loadDataFromDB:query]];

    //Logging for debug purpose
    NSLog(@"Result set starts");

    for(NSArray *subArray in dbResultArray) {
        NSLog(@"Data in array: %@",subArray);
    }

    NSLog(@"Result set ends");

    self.toDoItems = [[NSMutableArray alloc]initWithArray:dbResultArray];

    // Reload the table view.
    //[self.tblPeople reloadData];
    [self.tableView reloadData];
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.dbManager = [[DBManager alloc] initWithDatabaseFilename:@"todotaskdb.sql"];

    self.toDoItems = [[NSMutableArray alloc] init];
    [self loadInitialData];
}

And I am getting this error using NSLog

2014-11-25 18:24:13.862 ToDoList[3213:60b] Result set ends
2014-11-25 18:24:13.865 ToDoList[3213:60b] -[__NSArrayM itemName]: unrecognized selector sent to instance 0x8c996f0
2014-11-25 18:24:13.900 ToDoList[3213:60b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSArrayM itemName]: unrecognized selector sent to instance 0x8c996f0'
*** First throw call stack:
(
    0   CoreFoundation                      0x018dc1e4 __exceptionPreprocess + 180
    1   libobjc.A.dylib                     0x0165b8e5 objc_exception_throw + 44
    2   CoreFoundation                      0x01979243 -[NSObject(NSObject) doesNotRecognizeSelector:] + 275
    3   CoreFoundation                      0x018cc50b ___forwarding___ + 1019
    4   CoreFoundation                      0x018cc0ee _CF_forwarding_prep_0 + 14
    5   ToDoList                            0x00004882 -[XYZToDoListTableViewController tableView:cellForRowAtIndexPath:] + 306
    6   UIKit                               0x0041411f -[UITableView _createPreparedCellForGlobalRow:withIndexPath:] + 412
    7   UIKit                               0x004141f3 -[UITableView _createPreparedCellForGlobalRow:] + 69
    8   UIKit                               0x003f5ece -[UITableView _updateVisibleCellsNow:] + 2428
    9   UIKit                               0x0040a6a5 -[UITableView layoutSubviews] + 213
    10  UIKit                               0x0038a964 -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 355
    11  libobjc.A.dylib                     0x0166d82b -[NSObject performSelector:withObject:] + 70
    12  QuartzCore                          0x03d4745a -[CALayer layoutSublayers] + 148
    13  QuartzCore                          0x03d3b244 _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 380
    14  QuartzCore                          0x03d473a5 -[CALayer layoutIfNeeded] + 160
    15  UIKit                               0x0044cae3 -[UIViewController window:setupWithInterfaceOrientation:] + 304
    16  UIKit                               0x00362aa7 -[UIWindow _setRotatableClient:toOrientation:updateStatusBar:duration:force:isRotating:] + 5212
    17  UIKit                               0x00361646 -[UIWindow _setRotatableClient:toOrientation:updateStatusBar:duration:force:] + 82
    18  UIKit                               0x00361518 -[UIWindow _setRotatableViewOrientation:updateStatusBar:duration:force:] + 117
    19  UIKit                               0x003615a0 -[UIWindow _setRotatableViewOrientation:duration:force:] + 67
    20  UIKit                               0x0036063a __57-[UIWindow _updateToInterfaceOrientation:duration:force:]_block_invoke + 120
    21  UIKit                               0x0036059c -[UIWindow _updateToInterfaceOrientation:duration:force:] + 400
    22  UIKit                               0x003612f3 -[UIWindow setAutorotates:forceUpdateInterfaceOrientation:] + 870
    23  UIKit                               0x003648e6 -[UIWindow setDelegate:] + 449
    24  UIKit                               0x0043eb77 -[UIViewController _tryBecomeRootViewControllerInWindow:] + 180
    25  UIKit                               0x0035a474 -[UIWindow addRootViewControllerViewIfPossible] + 591
    26  UIKit                               0x0035a5ef -[UIWindow _setHidden:forced:] + 312
    27  UIKit                               0x0035a86b -[UIWindow _orderFrontWithoutMakingKey] + 49
    28  UIKit                               0x003653c8 -[UIWindow makeKeyAndVisible] + 65
    29  UIKit                               0x00315bc0 -[UIApplication _callInitializationDelegatesForURL:payload:suspended:] + 2097
    30  UIKit                               0x0031a667 -[UIApplication _runWithURL:payload:launchOrientation:statusBarStyle:statusBarHidden:] + 824
    31  UIKit                               0x0032ef92 -[UIApplication handleEvent:withNewEvent:] + 3517
    32  UIKit                               0x0032f555 -[UIApplication sendEvent:] + 85
    33  UIKit                               0x0031c250 _UIApplicationHandleEvent + 683
    34  GraphicsServices                    0x038d1f02 _PurpleEventCallback + 776
    35  GraphicsServices                    0x038d1a0d PurpleEventCallback + 46
    36  CoreFoundation                      0x01857ca5 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION__ + 53
    37  CoreFoundation                      0x018579db __CFRunLoopDoSource1 + 523
    38  CoreFoundation                      0x0188268c __CFRunLoopRun + 2156
    39  CoreFoundation                      0x018819d3 CFRunLoopRunSpecific + 467
    40  CoreFoundation                      0x018817eb CFRunLoopRunInMode + 123
    41  UIKit                               0x00319d9c -[UIApplication _run] + 840
    42  UIKit                               0x0031bf9b UIApplicationMain + 1225
    43  ToDoList                            0x00004e0d main + 141
    44  libdyld.dylib                       0x01e25701 start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException

Ironically, if put below line at the end of viewDidLoad method, then it works.

self.dbManager = [[DBManager alloc] initWithDatabaseFilename:@"todotaskdb.sql"];

Can anyone please tell me what is it that I am doing wrong?

1

There are 1 answers

0
regetskcob On BEST ANSWER

Your toDoItems contains Arrays which contains your items! You're may be getting arrays of the single rows in your database, so you would have to fill them into single toDoItems objects first.

Maybe you could use a factory-like pattern with a method like popToDoItemFromRow:(NSArray *)row which returns your toDoItem for this row.

Then fill an new array in your for (NSArray *subArray in dbResultArray) for these single rows. This new array should you use in you cellForRow...

may be like..

    - (void) loadInitialData{

    // Form the query.
    NSString *query = @"select * from tasklist";

    // Get the results.
    if (self.toDoItems != nil) {
        self.toDoItems = nil;
    }

    NSArray *dbResultArray = [[NSArray alloc] initWithArray:[self.dbManager loadDataFromDB:query]];

    if (self.toDoItems) {
        [self.toDoItems removeAllObjects];
    }
    else {
        self.toDoItems = [NSMutableArray new];
    }

    for(NSArray *subArray in dbResultArray) {
        [self.toDoItems addObject:[ToDoItem popToDoItemFromRow:subArray]];
    }

    // Reload the table view.
    //[self.tblPeople reloadData];
    [self.tableView reloadData];
}