4

In my application this error coming randomly when i send attachment(like Image and video) to other user,it crashes.

I am using Quickblox SDK.

This is the error..

NSRangeException', reason: '*** -[__NSArrayM objectAtIndex:]: index 1 beyond bounds [0 .. 0]

        2016-04-26 10:22:50.510 Sample-VideoChat[1027:12093] *** Terminating  app due to uncaught exception 'NSRangeException', reason:     '*** -[__NSArrayM objectAtIndex:]: index 1 beyond bounds [0 .. 0]'

  enter code here
  - (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
 {
     NSLog(@"self.chatSections....%@",self.chatSections);
     return [self.chatSections count];
 }


 - (UICollectionViewCell *)collectionView:(QMChatCollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
   {

    QBChatMessage *messageItem = [self messageForIndexPath:indexPath];

      NSRange textRange =[messageItem.text rangeOfString:@"https://api.quickblox.com/blobs/"];

     Class class = [self viewClassForItem:messageItem];
    NSString *itemIdentifier = [class cellReuseIdentifier];

     QMChatCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:itemIdentifier forIndexPath:indexPath];
cell.avatarView.tag = 0;

  if ((messageItem.attachments.count != 0) && textRange.location != NSNotFound) 
{

       QBChatAttachment* attachment = [[QBChatAttachment alloc] init];
         attachment.ID = @"3565772";
         attachment.url = messageItem.text;
         attachment.type = @"image";

         [cell.avatarView setImageWithURL:[NSURL URLWithString:attachment.url] placeholder:nil options:SDWebImageContinueInBackground progress:^(NSInteger receivedSize, NSInteger expectedSize)
     {
     } completedBlock:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL)
     {
         cell.avatarView.tag = 4592;
     }];
}

cell.transform = self.collectionView.transform;

UITapGestureRecognizer *singleTapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(singleTap:)];
singleTapGestureRecognizer.numberOfTapsRequired = 1;
singleTapGestureRecognizer.enabled = YES;

[self collectionView:collectionView configureCell:cell forIndexPath:indexPath];

return cell;
   }

 - (QBChatMessage *)messageForIndexPath:(NSIndexPath *)indexPath {
         if (indexPath.item == NSNotFound) {
          // If the update item's index path has an "item" value of NSNotFound, it means it was a section update, not an individual item.
        return nil;
     }

      QMChatSection *currentSection = self.chatSections[indexPath.section];
     return currentSection.messages[indexPath.item];
     }
PPreeti
  • 201
  • 1
  • 3
  • 10
  • Add some code to solve this error.@preeti patel – Mr. Bond Apr 26 '16 at 05:45
  • You can find code below the error. – PPreeti Apr 26 '16 at 06:19
  • Try adding an exception breakpoint (Add Exception Breakpoint) to debug where the exception occures. It does mean you are trying to access an object which is not in the index of array. – Mr. Bond Apr 26 '16 at 06:23
  • In this method - (QBChatMessage *)messageForIndexPath:(NSIndexPath *)indexPath getting error to return currentSection. But sometimes it execute code. – PPreeti Apr 26 '16 at 06:32
  • your item value should be nil, so app is carsh. plz check your item value. – Mr. Bond Apr 26 '16 at 06:37
  • Please check you have values in 'self.chatSections' and 'currentSection.messages' before doing the further operations – HardikDG Apr 26 '16 at 06:41
  • @preetipatel Please check your method (Number of item in collection view). i think you created number of item with different array count which you use in cell for item – Parvendra Singh Apr 26 '16 at 07:29

4 Answers4

23

Try adding an all-exception breakpoint (Add Exception Breakpoint) to debug where the exception occures. It does mean you are trying to access an object which is not in the index of array.

Best to find what is causing problem is to raise all exceptions before app crashes.

enter image description herei

Balu mallisetty
  • 603
  • 9
  • 19
5

Static cells, I had the same issue. So, I just replaced the tableView content type

"Static cell" to "Dynamic prototypes".

Image

Rashid Latif
  • 2,809
  • 22
  • 26
0

try this:

if (item != nil && item.count != 0) {
    QMChatSection *currentSection = self.chatSections[indexPath.section];
    return currentSection.messages[indexPath.item];
}
mangerlahn
  • 4,746
  • 2
  • 26
  • 50
Mr. Bond
  • 427
  • 1
  • 4
  • 18
  • 1
    @preetipatel Please check your method (Number of item in collection view). i think you created number of item with different array count which you use in cell for item – Parvendra Singh Apr 26 '16 at 07:28
0

You can make check like this, check for the value before you try to access the index

  if (self.chatSections.count > 0) {
        QMChatSection *currentSection = self.chatSections[indexPath.section];
        if (currentSection.messages.count > 0){
            return currentSection.messages[indexPath.item];
        }else {
            return nil;
        }
    } else {
        return nil;
    }

If you are creating your own code with Quickblox you can refer their demo implementation as for me it works proper

HardikDG
  • 5,892
  • 2
  • 26
  • 55