Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

setIdentificationAttributes :- Date property #2524

Open
hitendradeveloper opened this issue Nov 6, 2017 · 0 comments
Open

setIdentificationAttributes :- Date property #2524

hitendradeveloper opened this issue Nov 6, 2017 · 0 comments

Comments

@hitendradeveloper
Copy link

hitendradeveloper commented Nov 6, 2017

Hi I want to set the timestamp attribute of my message object as IdentificationAttributes.

I tried as below, before I was using the messageID as the identification attribute and it was working. but now I want to use the timestamp as identification.

+ (nonnull RKEntityMapping *)mappingInManagedObjectStore:(nonnull RKManagedObjectStore *)managedObjectStore
{
	// Defines mapping for C8UserBrief
	RKEntityMapping *mapping = [RKEntityMapping mappingForEntityForName:C8EntityNameForChatMessage
												   inManagedObjectStore:managedObjectStore];

    [mapping setIdentificationAttributes:@[@"xmppMessageID"]];


    
	[mapping addAttributeMappingsFromDictionary:@{
                                                  @"id" : @"messageID",
                                                  @"xmpp_message_ID" : @"xmppMessageID",
			@"timestamp" : @"timestamp",
			@"sender_id" : @"senderID",
			@"message" : @"message",
			@"media_id" : @"mediaID",
			@"trip_id" : @"tripID",
            @"type" : @"type",
            @"start_date":@"startDate",
            @"end_date":@"finishDate"}];


	// Adds nested objects
	[mapping addConnectionForRelationship:@"sender" connectedBy:@{@"senderID" : @"userID"}];
	[mapping addConnectionForRelationship:@"media" connectedBy:@{@"mediaID" : @"mediaID"}];
	[mapping addConnectionForRelationship:@"trip" connectedBy:@{@"tripID" : @"tripID"}];

	return mapping;
}

Below is my code which saves the object manually when I am calling from XMPP delegate.

+(C8ChatMessage *)saveC8ChatMessageFrom:(XMPPMessage *)xmppMessage {
    
    //[self testMappingStringIdentificationAttributesFromElementsOnAnArrayDoesNotDuplicateManagedObjects];
    
    
    
    NSManagedObjectContext *privateContext = [[[RKObjectManager sharedManager] managedObjectStore] mainQueueManagedObjectContext];
    
    id<RKManagedObjectCaching> managedObjectCache = [[[RKObjectManager sharedManager] managedObjectStore] managedObjectCache];
    
    NSLog(@"\n\nSaving received xmppMessage-C8ChatMessage := %@\n",xmppMessage);
    C8ChatMessage *message;{
        NSArray *fetchedObjects;
        
        NSFetchRequest *fetch = [[NSFetchRequest alloc] init];
        NSEntityDescription *entityDescription = [NSEntityDescription entityForName:C8EntityNameForChatMessage  inManagedObjectContext: privateContext];
        [fetch setEntity:entityDescription];
        [fetch setPredicate:[NSPredicate predicateWithFormat:@"(xmppMessageID == %@)",message.senderID]];
        NSError * error = nil;
        fetchedObjects = [privateContext executeFetchRequest:fetch error:&error];
        if([fetchedObjects count] == 1){
            message = [fetchedObjects objectAtIndex:0];
        }else{
            message = (C8ChatMessage *)[NSEntityDescription
                                        insertNewObjectForEntityForName:C8EntityNameForChatMessage
                                        inManagedObjectContext:privateContext];
        }
    }
    
    
    message.messageID = [xmppMessage attributeNumberInt64ValueForName:@"id"];
    message.xmppMessageID = [xmppMessage attributeStringValueForName:@"xmpp_message_ID"];    
    
    message.timestamp = [NSDate dateWithTimeIntervalSince1970:
                         [xmppMessage attributeDoubleValueForName:@"timestamp"]
                         ];
    
    NSLog(@"Received timeStamp:= %@",[xmppMessage attributeNumberDoubleValueForName:@"timestamp"]);
    NSLog(@"Received xmppMessageID:= %@",[xmppMessage attributeStringValueForName:@"xmpp_message_ID"]);
    
    message.senderID = [xmppMessage attributeNumberInt64ValueForName:@"sender_id"];
    
    NSString *messageStr = [xmppMessage attributeStringValueForName:@"message"];
    message.message = [NSString stringWithFormat:@"%@ from xmpp",messageStr];
    
    message.mediaID = [xmppMessage attributeNumberInt64ValueForName:@"media_id"];
    message.tripID = [xmppMessage attributeNumberInt64ValueForName:@"trip_id"];
    message.type = [xmppMessage attributeNumberIntValueForName:@"c8_type"];
    
    message.startDate = [NSDate dateWithTimeIntervalSince1970:
                         [xmppMessage attributeNumberFloatValueForName:@"start_date"].doubleValue
                         ];
    message.finishDate = [NSDate dateWithTimeIntervalSince1970:
                         [xmppMessage attributeNumberFloatValueForName:@"end_date"].doubleValue
                         ];
    
    
    C8MediaDescription *mediaDescription;
    if([C8MediaDescription isValid:xmppMessage]){
        mediaDescription = [C8MediaDescription getC8MediaDescriptionFrom:xmppMessage];
        [managedObjectCache didCreateObject:mediaDescription];
    }
    
    C8TripData *trip;{
        NSArray *fetchedObjects;
        
        NSFetchRequest *fetch = [[NSFetchRequest alloc] init];
        NSEntityDescription *entityDescription = [NSEntityDescription entityForName:C8EntityNameForTripData  inManagedObjectContext: privateContext];
        [fetch setEntity:entityDescription];
        [fetch setPredicate:[NSPredicate predicateWithFormat:@"(tripID == %@)",message.tripID]];
        NSError * error = nil;
        fetchedObjects = [privateContext executeFetchRequest:fetch error:&error];
        if([fetchedObjects count] == 1){
            trip = [fetchedObjects objectAtIndex:0];
            [managedObjectCache didFetchObject:trip];
        }
    }
    
    C8UserBrief *user;{
        NSArray *fetchedObjects;
        
        NSFetchRequest *fetch = [[NSFetchRequest alloc] init];
        NSEntityDescription *entityDescription = [NSEntityDescription entityForName:C8EntityNameForUserBrief  inManagedObjectContext: privateContext];
        [fetch setEntity:entityDescription];
        [fetch setPredicate:[NSPredicate predicateWithFormat:@"(userID == %@)",message.senderID]];
        NSError * error = nil;
        fetchedObjects = [privateContext executeFetchRequest:fetch error:&error];
        if([fetchedObjects count] == 1){
            user = [fetchedObjects objectAtIndex:0];
            [managedObjectCache didFetchObject:user];
        }
        
    }
    
    //C8ChatMessage Relationship
    message.trip = trip;
    message.sender = user;
    
    //C8MediaDescription Relationship
    if (mediaDescription){
        if(trip){
            [mediaDescription addTripsObject:trip];
        }
        mediaDescription.messages = message;
    }
    
    [managedObjectCache didCreateObject:message];
    [privateContext saveToPersistentStore:nil];
//    [managedObjectCache didFetchObject:message];
    
    NSString *strTripJIDKey = [NSString stringWithFormat:@"lastclear%@",[message.trip jid]];
    NSData *date = (NSData *)[NSDate date];
    XSET_OBJECT(strTripJIDKey, date)
    
    return message;
}

Any help is appreciated.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant