Realm-cocoa: problem 'Index 1 is out of bounds (must be less than 1)'

Created on 1 Jan 2017  路  11Comments  路  Source: realm/realm-cocoa

Hello guys , i have a project build in swift 3 & xc 8.2 beta and realm last version by cocoapod ' before 2.1.2'

the problem is i get data from realm db, first loop i find that object i want, and next loop i find it as well, but suddenly app will be crashed some how when i show in table view .

here is the crash details :

 *** Terminating app due to uncaught exception 'RLMException', reason: 'Index 1 is out of bounds (must be less than 1)'
*** First throw call stack:
(
    0   CoreFoundation                      0x000000010c8fad4b __exceptionPreprocess + 171
    1   libobjc.A.dylib                     0x000000010c35c21e objc_exception_throw + 48
    2   Realm                               0x0000000109c1b266 _ZL10throwErrorP8NSString + 646
    3   Realm                               0x0000000109c17f7a _ZL15translateErrorsIZ28-[RLMResults objectAtIndex:]E3$_3EDaOT_P8NSString + 106
    4   Realm                               0x0000000109c17f08 -[RLMResults objectAtIndex:] + 56
    5   RealmSwift                          0x000000010a385ff6 _TFC10RealmSwift7Resultsg9subscriptFSix + 118
    6   BeautGuide                          0x0000000107589ca0 _TFC10BeautGuide21providers_review_view9tableViewfTCSo11UITableView12cellForRowAtV10Foundation9IndexPath_CSo15UITableViewCell + 2384
    7   BeautGuide                          0x000000010758a467 _TToFC10BeautGuide21providers_review_view9tableViewfTCSo11UITableView12cellForRowAtV10Foundation9IndexPath_CSo15UITableViewCell + 87
    8   UIKit                               0x000000010a7443f8 -[UITableView _createPreparedCellForGlobalRow:withIndexPath:willDisplay:] + 757
    9   UIKit                               0x000000010a744656 -[UITableView _createPreparedCellForGlobalRow:willDisplay:] + 74
    10  UIKit                               0x000000010a718124 -[UITableView _updateVisibleCellsNow:isRecursive:] + 3295
    11  UIKit                               0x000000010a74d9d8 -[UITableView _performWithCachedTraitCollection:] + 110
    12  UIKit                               0x000000010a734232 -[UITableView layoutSubviews] + 222
    13  UIKit                               0x000000010a69b92a -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 1237
    14  QuartzCore                          0x0000000109864bf8 -[CALayer layoutSublayers] + 146
    15  QuartzCore                          0x0000000109858440 _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 366
    16  UIKit                               0x000000010a68999c -[UIView(Hierarchy) layoutBelowIfNeeded] + 1509
    17  UIKit                               0x000000010a7cc7b8 -[UITabBarController _layoutViewController:] + 739
    18  UIKit                               0x000000010a7cc8fa -[UITabBarController _wrapperViewForViewController:] + 252
    19  UIKit                               0x000000010a7d73f6 -[UITabBarController transitionFromViewController:toViewController:transition:shouldSetSelected:] + 626
    20  UIKit                               0x000000010a7d67a9 -[UITabBarController transitionFromViewController:toViewController:] + 59
    21  UIKit                               0x000000010a7d266a -[UITabBarController _setSelectedViewController:] + 365
    22  UIKit                               0x000000010a7d24ec -[UITabBarController setSelectedViewController:] + 234
    23  UIKit                               0x000000010a7d6656 -[UITabBarController _tabBarItemClicked:] + 369
    24  UIKit                               0x000000010a5d880c -[UIApplication sendAction:to:from:forEvent:] + 83
    25  UIKit                               0x000000010a9f1ae0 -[UITabBar _sendAction:withEvent:] + 579
    26  UIKit                               0x000000010a5d880c -[UIApplication sendAction:to:from:forEvent:] + 83
    27  UIKit                               0x000000010a75eaac -[UIControl sendAction:to:forEvent:] + 67
    28  UIKit                               0x000000010a75edc5 -[UIControl _sendActionsForEvents:withEvent:] + 444
    29  UIKit                               0x000000010a9f41f0 -[UITabBar _buttonUp:] + 113
    30  UIKit                               0x000000010a5d880c -[UIApplication sendAction:to:from:forEvent:] + 83
    31  UIKit                               0x000000010a75eaac -[UIControl sendAction:to:forEvent:] + 67
    32  UIKit                               0x000000010a75edc5 -[UIControl _sendActionsForEvents:withEvent:] + 444
    33  UIKit                               0x000000010a75dcc1 -[UIControl touchesEnded:withEvent:] + 668
    34  UIKit                               0x000000010a646495 -[UIWindow _sendTouchesForEvent:] + 2747
    35  UIKit                               0x000000010a647b83 -[UIWindow sendEvent:] + 4011
    36  UIKit                               0x000000010a5f48fb -[UIApplication sendEvent:] + 371
    37  UIKit                               0x000000010ade074d __dispatchPreprocessedEventFromEventQueue + 3248
    38  UIKit                               0x000000010add9483 __handleEventQueue + 4879
    39  CoreFoundation                      0x000000010c89f761 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17
    40  CoreFoundation                      0x000000010c88498c __CFRunLoopDoSources0 + 556
    41  CoreFoundation                      0x000000010c883e76 __CFRunLoopRun + 918
    42  CoreFoundation                      0x000000010c883884 CFRunLoopRunSpecific + 420
    43  GraphicsServices                    0x0000000111de3a6f GSEventRunModal + 161
    44  UIKit                               0x000000010a5d6bb8 UIApplicationMain + 159
    45  BeautGuide                          0x0000000107590b0f main + 111
    46  libdyld.dylib                       0x000000010d82568d start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException

is it a bug in realm or xcode ?
note : other elements is appearing as appropriate way .

T-Help

Most helpful comment

Little extension for easiness

extension Results {
    subscript (save index: Int) -> Element? {
        if index < self.count {
            return self[index]
        } else {
            return nil
        }
    }
}

if let post = posts?[save: indexPath.row] {
    cell.postMini = post
}

or

   func saveFetch(_ index: Int) -> Element? {
        if index < self.count {
            return self[index]
        } else {
            return nil
        }
    }

All 11 comments

and of course the object i made 'for loop' for it is not empty

Thanks for getting in touch with us. Can you provide a project that reproduces this behavior? You haven't provided enough information for us to figure out what is going on, unfortunately.

fortunate i fix the problem after hours of checking .

the problem is : when use two objects from any kind of realm class 'Results' in func 'cellForRowAt', with '[indexPath.row]' this crash will happens.

this problem from realm , i thing it's a bug .

i'm trying helping , but unfortunate i couldn't share the project because of police of the client :(
For any other info feel free to contact .

The exception you're seeing is telling you that you're accessing past the end of a Results object (index 1 of a Results that contains only one object). This invalid access happens in providers_review_view.tableView(_:cellForRowAt:).

thanks for replying ,
i will explain as much as i can to be the picture clear for you .

i have an Results appear in in table view , the code will be like this :

cell.cityname.text = array_of_cities[indexPath.row].name

in this table i have a field called country id, and i will get the country name from country id , so the code will be like this :

cell.countryname.tect = anyObject.get_cuntry_by_id(id: array_of_cities[indexPath.row].id)[indexPath.row].name

is above code will be the crash , of course this is an example, but when i use same logic with any 2 different array it's working fine this problem came only when i use realm .

Sorry for a delay with the reply, @ssam2000. When requesting an object from the Result by its index please make sure that that index is present in the collection in other case you'll get an exception.

You can check it just by checking that the requested index is less than the number of objects in the collection:

if indexPath.row < array_of_cities.count {
    cell.cityname.text = array_of_cities[indexPath.row]
} else {
    // Handle non-existing object here
}

Regarding to your case it's seems like there is no object at indexPath.row either in array_of_cities or in the collection returned from anyObject.get_cuntry_by_id.

@ssam2000 did you solve this problem or do you need any further help with this?

Please keep in mind that refreshing the Realm to the latest state can invalidate objects (or change the members in a List/RLMArray). Please see our docs on "Seeing Changes from Other Threads" for more information.

Closing due to inactivity. @ssam2000: Feel free to follow-up and we can re-open if necessary.

Thanks. This fixed my issue with index out of range.

Little extension for easiness

extension Results {
    subscript (save index: Int) -> Element? {
        if index < self.count {
            return self[index]
        } else {
            return nil
        }
    }
}

if let post = posts?[save: indexPath.row] {
    cell.postMini = post
}

or

   func saveFetch(_ index: Int) -> Element? {
        if index < self.count {
            return self[index]
        } else {
            return nil
        }
    }
Was this page helpful?
0 / 5 - 0 ratings