title: 'attempt to scroll to invalid index path: <NSIndexPath: 0x6000002264a0> {length = 2, path = 0 - 0}'
body: <p>I am new to Swift and Realm, sorry. I am trying to build a Scrollable Tab Pages, wherein all data comes from Realm. </p>
  
  <p>First Launch, after I successfully login: The data is not loaded automatically. </p>
  
  <p>Second Launch, I am getting this error.</p>
  
  <pre><code>'NSInvalidArgumentException', reason: 'attempt to scroll to invalid index path: &lt;NSIndexPath: 0x6000002264a0&gt; {length = 2, path = 0 - 0}'
  </code></pre>
  
  <p>I tested my code using static String Arrays, it works well. Actually, this coded was also taken from somewhere...   </p>
  
  <p>This is my ViewController:</p>
  
  <pre><code>class ViewController: UIViewController{
  
           let tabs = try! Realm().objects(Tabs)
  
           //after successful login. begin collecting Tab Items
           override func viewDidLoad() {
              WebController().getTabs()
              buildTabItems()
           }
  
          override func viewWillAppear(_ animated: Bool) {
              super.viewWillAppear(animated)
  
              view.layoutIfNeeded()
              buildSamplePages()
  
              let firstPageIndex = 2
  
              focusView.layer.cornerRadius = (scrollingTabBar.height - scrollingTabBar.focusVerticalMargin * 2.0) / 2.0
              scrollView.contentSize = CGSize(width: view.width * CGFloat(tabItems.count), height: scrollView.contentSize.height)
              scrollingTabBar.width = view.width
              scrollingTabBar.pageWidth = scrollView.width
              scrollingTabBar.reloadTabs(tabItems, indexOf: firstPageIndex)
              scrollView.setContentOffset(CGPoint(x: scrollView.width * CGFloat(firstPageIndex), y: scrollView.contentOffset.y), animated: false)
  
          }
  
  
         //collect Tab Items from Realm
         func buildTabItems() {
  
              let array = Array(tabs).description
              var strings = [String]()
  
              for tabData in tabs{
                  strings.append(tabData.title)
              }
  
  
              for str in strings {
                  let tabItem = USGScrollingTabItem()
                  tabItems.append(tabItem)
              }
          }
  
         //display Pages
          func buildTabPages() {
              for (pageNum, _) in tabItems.enumerated() {
                  let label = UILabel(frame: CGRect.zero)
                  label.text = "i am a test\(pageNum)"
                  scrollView.addSubview(label)
              }
          }
  
  
      }
  
      extension ViewController: USGScrollingTabBarDelegate {
  
          func tabBar(_ tabBar: USGScrollingTabBar, didSelectTabAt index: Int) {
              scrollView.setContentOffset(CGPoint(x: scrollView.width * CGFloat(index), y: scrollView.contentOffset.y), animated: true)
          }
      }
  
      extension ViewController: UIScrollViewDelegate {
  
          func scrollViewDidScroll(_ scrollView: UIScrollView) {
              scrollingTabBar.enabled = !scrollView.isTracking;
  
              if (scrollView.isTracking || scrollView.isDecelerating) {
                  scrollingTabBar.scrollToOffset(scrollView.contentOffset.x)
              }
          }
  
          func scrollViewWillBeginDragging(_ scrollView: UIScrollView) {
              scrollingTabBar.stopScrollDeceleration()
              scrollingTabBar.enabled = false
          }
  
          func scrollViewDidEndDragging(_ scrollView: UIScrollView, willDecelerate decelerate: Bool) {
              scrollingTabBar.enabled = true
          }
      }
  </code></pre>
  
  <p>This is my WebService Code</p>
  
  <pre><code>class WebController{
      let a = UserDefaults.standard.string(forKey: "a")
      let b = UserDefaults.standard.string(forKey: "b")
      let c = UserDefaults.standard.integer(forKey: "c")
      let d = UserDefaults.standard.string(forKey: "d")
  
      func  getTabs() {
  
          let reqParams: Parameters = ["a": a ,"b": b]
          let header = ["c":"c, "d":"d"]
          let url = APIConstant.URLS().TabsURL
  
          guard let realm = try? Realm() else {
              return
          }
  
          Alamofire.request(url, method: HTTPMethod.post, parameters:reqParams, headers:header).responseJSON { response in
  
          guard response.result.isSuccess, let value = response.result.value else {
                  return
              }
              let json = JSON(value)
              let tabs = json["result"]
              realm.beginWrite()
              for (_, subJson) : (String, JSON) in tabs {
                  let tabs: Tabs = Mapper&lt;Tabs&gt;().map(JSONObject: subJson.dictionaryObject!)!
                  realm.add(tabs, update: true)
              }
              do {
                  try realm.commitWrite()
              } catch {
  
              }
          }
  
      }
  
  }
  </code></pre>
  
  <p>Anyone can give me an idea, how to load a Realm Data automatically after I login (or launch) the page? </p>