2

This is just the Core-Data object for message:

#import <Foundation/Foundation.h>
#import <CoreData/CoreData.h>

@class Account;

@interface Message : NSManagedObject

@property (nonatomic, retain) NSNumber * read;
@property (nonatomic, retain) NSDate * sentDate;
@property (nonatomic, retain) NSString * text;
@property (nonatomic, retain) NSString * receiver;
@property (nonatomic, retain) NSString * sender;
@property (nonatomic, retain) Account *account;

@end

Then this is the Messages ViewController, load messages code:

- (void)loadMessages


messages = [[NSMutableArray alloc]initWithArray:_fetchedResultsController.fetchedObjects];



//------------------------------------------------------------------------------------------------------------------------------------------------- Demo message just demo an incoming message.

static NSString *senderId = @"2";
static NSString *senderDisplayName = @"a";


JSQMessage *message = [[JSQMessage alloc] initWithSenderId:senderId
                                         senderDisplayName:senderDisplayName
                                                      date:[NSDate distantPast]
                                                      text:@"helloloo"];

messages = [[NSMutableArray alloc]initWithObjects:message, nil];

//------------------------------------------------------------------------------------------------------------------------------------------------- Demo message just demo an incoming message.

Now I'm new to this framework https://github.com/jessesquires/JSQMessagesViewController , I went all over the framework docs and didn't find answer for my problem which is:

Can I use (NSFetchedResultsController) with this framework? If so how can I use the (NSFetchedResultsController) in the JSQMessages CollectionView DataSource in the right way and return all my Messages from my core data?. I know how to use (NSFetchedResultsController) but not so sure how to use it with the CollectionView and with this framework. It seem like the CollectionView data source/delegate methods only work with NSmutableArray and the app crashes when I do staff with (NSFetchedResultsController).

For example:

return [_fetchedResultsController.fetchedObjects objectAtIndex:indexPath.item];

VS:

return messages[indexPath.item]; 

When I use the _fetchedResultsController.fetchedObjects my app crashes.

So after thinking what to do I said ok what if I will convert my .fetchedObjects (NSarray) to be my (NSmutablearray) of the messages and then I got stack I just don't know how to do this the right way I need help. All I need is just a way to return all my messages from my SQlite database to my app.

I also have looked at Parse example https://github.com/relatedcode/RealtimeChat , here but I want my own Code without having to pay to Parse.com and to be able to use Core-Data.

When I call this : JSQMessage I need some how to tell it to return all my objects in my fetch result controller *.fetechedobjects*.

Edwin Vermeer
  • 13,017
  • 2
  • 34
  • 58

0 Answers0