lundi 7 juillet 2014

Core Data keeps two objects instead 27 in fetchObjects


Vote count:

0




First step: Upload from Json data(total of data = 27 objects) then i want keep all this objects in my Core Data; Second Step: After time period i checking my Json again for new objects. Compare Core Data objects count with Json objects count, if its different i going to add this objects to my Core Data. But my Core Data has a wrong objects count on the second step(2 objects in fetchObjects). In summary i would like to copy all objects from Json to my Core Data and each ten seconds i check for new objects in my Json. Here is my following code:



- (NSInteger) isNewData: (NSArray *) fetchedObjects
{
///////////json part
NSURLRequest* urlRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:getMessageURL] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];
NSData * data = [NSURLConnection sendSynchronousRequest:urlRequest returningResponse:nil error:nil];
_json = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];
///////////core data part
//NSManagedObjectContext *context = [self managedObjectContext];
/*NSError *error;
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription
entityForName:@"Entity" inManagedObjectContext:context];
[fetchRequest setEntity:entity];
NSArray *fetchedObjects = [context executeFetchRequest:fetchRequest error:&error];*/
/////////if part
NSLog(@"%d",fetchedObjects.count);
return _json.count - fetchedObjects.count;
}

- (void) loadDataToCoreDataFromJson: (NSInteger) odds andSecond: (NSManagedObjectContext *) context
{
//core data connection
Entity *newList = [NSEntityDescription
insertNewObjectForEntityForName:@"Entity"
inManagedObjectContext:context];
newList = [NSEntityDescription insertNewObjectForEntityForName:@"Entity" inManagedObjectContext:context];
for (int i = 0; i < odds; i++)//?
{
//NSManagedObjectContext *context = [self managedObjectContext];

newList.login = [[_json objectAtIndex:_json.count - odds + i] objectForKey:@"login"];
newList.message = [[_json objectAtIndex:_json.count - odds + i] objectForKey:@"message"];
newList.date = [[_json objectAtIndex:_json.count - odds + i] objectForKey:@"date"];
[context save:nil];
}

}

- (void) loadDataFromCoreDataToArrays : (NSArray*) fetchedObjects
{
_getDate = [[NSMutableArray alloc] init];
_getMessage = [[NSMutableArray alloc] init];
_getLogin = [[NSMutableArray alloc] init];
///////////core data part connection
//NSManagedObjectContext *context = [self managedObjectContext];
for (int i = 0; i < fetchedObjects.count; i++)
{
Entity * info = [fetchedObjects objectAtIndex:i];
[_getLogin addObject:info.login];
[_getMessage addObject:info.message];
[_getDate addObject:info.date];
}

}

- (void) chating
{
AppDelegate* appDelegate = [UIApplication sharedApplication].delegate;
NSManagedObjectContext *context = appDelegate.managedObjectContext;
NSError *error;
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription
entityForName:@"Entity" inManagedObjectContext:context];
[fetchRequest setEntity:entity];
NSArray *fetchedObjects = [context executeFetchRequest:fetchRequest error:&error];
NSInteger odds = [self isNewData: fetchedObjects]; //0 if no data, if > 0 then there are N new data;
if (odds != 0)
{
AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);
[self loadDataToCoreDataFromJson: odds andSecond:context];
[self loadDataFromCoreDataToArrays: fetchedObjects];
[self reloadMyTableView];
}
}

- (void) viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
_buttonSend.enabled = NO;
[self loadUserDefault];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
_connects++;
NSLog(@"return message");
dispatch_async(dispatch_get_main_queue(), ^{
[NSTimer scheduledTimerWithTimeInterval:10 target:self selector:@selector(chating) userInfo:nil repeats:YES];
[NSTimer scheduledTimerWithTimeInterval:10 target:self selector:@selector(reloadMyTableView) userInfo:nil repeats:YES];
});
});
}


I think i doing wrong with Core Data. Thanks every body for help.




rmaddy

74k

asked 24 secs ago






Aucun commentaire:

Enregistrer un commentaire