I'm doing a project using core data, and as far as I know anything done in the core data thread has to remain in said thread.
I do a call to an API to download some news items and then load them into the database:
[self.database.managedObjectContext performBlock:^{
for (NSDictionary *itemInfo in result) {
NSLog(#"%#", itemInfo);
[Item createItemWithInfo:itemInfo inManagedObjectContext:self.database.managedObjectContext];
}
[self.database.managedObjectContext save:nil];
}];
In my create method, besides setting up all the data in the objects I have an additional call to get an image related to the news item in question:
+ (Item *)createItemWithInfo:(NSDictionary *)info inManagedObjectContext:(NSManagedObjectContext *)context {
Item *item;
NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:#"Item"];
request.predicate = [NSPredicate predicateWithFormat:#"itemId = %#", [info valueForKeyPath:#"News.id_contennoticias"]];
NSSortDescriptor *sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:#"itemId" ascending:YES];
request.sortDescriptors = [NSArray arrayWithObject:sortDescriptor];
NSError *error = nil;
NSArray *matches = [context executeFetchRequest:request error:&error];
if (!matches || ([matches count] > 1)) {
// handle error
} else if ([matches count] == 0) {
item = [NSEntityDescription insertNewObjectForEntityForName:#"Item" inManagedObjectContext:context];
item.itemId = [NSNumber numberWithInteger:[[info valueForKeyPath:#"News.id_contennoticias"] integerValue] ];
item.title = [info valueForKeyPath:#"News.titulo_contennoticias"];
item.summary = [info valueForKeyPath:#"News.sumario_contennoticias"];
item.content = [info valueForKeyPath:#"News.texto_contennoticias"];
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
dateFormat.dateFormat = #"yyyy-MM-dd hh:mm:ss";
NSDate *creationDate = [dateFormat dateFromString:[info valueForKeyPath:#"News.fechacre_contennoticias"]];
item.creationDate = creationDate;
dispatch_queue_t imageDownloadQueue = dispatch_queue_create("image downloader", NULL);
dispatch_async(imageDownloadQueue, ^{
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:#"%#/files/imagenprincipal/%#", BASE_PATH, [info valueForKeyPath:#"News.imgprincipal"]]];
NSData *imageData = [NSData dataWithContentsOfURL:url];
dispatch_async(dispatch_get_current_queue(), ^{
item.image = imageData;
});
});
} else {
item = [matches lastObject];
}
return item;
}
On this part:
dispatch_async(dispatch_get_current_queue(), ^{
item.image = imageData;
});
I'm getting the error, my app just dies there. Also it says that dispatch_get_current_queue() is deprecated in iOS 6.
Since dispatch_get_current_queue() is called from within a block on the imageDownloadQueue, why aren't you using imageDownloadQueue directly? If you want to run it on the moc queue, then make sure not to use dispatch_get_current_queue().
In general be careful with using dispatch_get_current_queue(). Quoting from the Dispatch Queues page in the Apple Concurrency Programming guide:
Use the dispatch_get_current_queue function for debugging purposes or
to test the identity of the current queue.
Related
I am newbie in Core Data. I put all model requirements (in respect of MVC) in a class of NSFetchedResultsController as below:
Header File :
#import <CoreData/CoreData.h>
#interface GeneralModel : NSFetchedResultsController
#property (nonatomic, strong) NSManagedObjectContext *context;
#property (nonatomic, strong) NSManagedObjectModel *model;
- (NSString *)storagePath;
- (void)removeStorage;
- (void)truncateAllEntity;
- (void)truncateEntity:(NSString *)entityName;
- (void)addGroups:(NSDictionary *)insertData;
- (NSArray *)getEntity:(NSString *)entityName sortBy:(NSString *)sortAttribute;
- (id)getMaxValue:(NSString *)entityName forProperty:(NSString *)propertyName;
- (NSArray *)getEntity:(NSString *)entityName predicateBy:(NSPredicate *)predicate sortBy:( NSString * )sortAttribute;
#end
Main File:
#import "GeneralModel.h"
#import "GeneralHelper.h"
#implementation GeneralModel
- (instancetype)init
{
self = [super init];
if (self) {
// Read in Model.xcdatamodeld
_model = [NSManagedObjectModel mergedModelFromBundles:nil];
NSPersistentStoreCoordinator *psc =
[[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:_model];
// Where does the SQLite file go?
NSString *path = self.storagePath;
NSURL *storeURL = [NSURL fileURLWithPath:path];
NSError *error = nil;
if (![psc addPersistentStoreWithType:NSSQLiteStoreType
configuration:nil
URL:storeURL
options:nil
error:&error]) {
#throw [NSException exceptionWithName:#"OpenFailure"
reason:[error localizedDescription]
userInfo:nil];
}
// Create the managed object context
_context = [[NSManagedObjectContext alloc] init];
_context.persistentStoreCoordinator = psc;
}
return self;
}
- (NSString *)storagePath
{
NSArray *documentDirectories =
NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask,
YES);
// Get one and only document directory from that list
NSString *documentDirectory = [documentDirectories firstObject];
return [documentDirectory stringByAppendingPathComponent:#"model.sqlite"];
}
- (void)removeStorage {
// NSError *error2;
// NSString *storagePath = [self storagePath];
//
// NSDictionary *options = #{NSPersistentStoreUbiquitousContentNameKey: #"model"};
// bool removeResult = [NSPersistentStoreCoordinator removeUbiquitousContentAndPersistentStoreAtURL:[NSURL URLWithString:storagePath] options:options error:&error2];
// if (removeResult == NO) {
// NSLog(#"Could not remove Storage. Reason: %#", error2.localizedFailureReason);
// }
NSPersistentStore *store = [self.context.persistentStoreCoordinator.persistentStores lastObject];
NSError *error = nil;
NSURL *storeURL = store.URL;
BOOL isRemovePersistentStore = [self.context.persistentStoreCoordinator removePersistentStore:store error:&error];
if (isRemovePersistentStore == NO) {
NSLog(#"NO RemovePersistentStore. Reason: %#", error.localizedFailureReason);
}
BOOL isRemoveItemAtURL = [[NSFileManager defaultManager] removeItemAtURL:storeURL error:&error];
if (isRemoveItemAtURL == NO) {
NSLog(#"NO RemoveItemAtURL. Reason: %#", error.localizedFailureReason);
}
}
- (void)truncateAllEntity {
NSArray *entities = self.model.entities;
for (NSEntityDescription *entityDescription in entities) {
[self truncateEntity:entityDescription.name];
}
}
- (void)truncateEntity:(NSString *)entityName {
// delete all database
if (IOS_VERSION >= 9) {
NSFetchRequest *request = [[NSFetchRequest alloc] initWithEntityName:entityName];
NSBatchDeleteRequest *delete = [[NSBatchDeleteRequest alloc] initWithFetchRequest:request];
NSError *deleteError = nil;
[self.context.persistentStoreCoordinator executeRequest:delete withContext:self.context error:&deleteError];
} else {
NSFetchRequest *allItems = [[NSFetchRequest alloc] init];
[allItems setEntity:[NSEntityDescription entityForName:entityName inManagedObjectContext:self.context]];
[allItems setIncludesPropertyValues:NO]; //only fetch the managedObjectID
NSError *error = nil;
NSArray *items = [self.context executeFetchRequest:allItems error:&error];
//error handling goes here
for (NSManagedObject *item in items) {
[self.context deleteObject:item];
}
NSError *saveError = nil;
[self.context save:&saveError];
//more error handling here
}
[self.context rollback];
}
- (void)addGroups:(NSDictionary *)insertData {
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd HH:mm:ss"];
for (NSDictionary *response in insertData) {
NSManagedObject *existingGroup = [self getEntity:#"Group" AtValue:response[#"groupId"] forProperty:#"group_id" ];
if (existingGroup) {
NSLog(#"existingGroup");
continue;
}
// for (int i; i<=20; i++) {
// set bullet
NSManagedObject *object_bullet = [NSEntityDescription insertNewObjectForEntityForName:#"Group_bullet"
inManagedObjectContext:self.context];
NSData *bulletData = [[NSData alloc] initWithBase64EncodedString:response[#"bullet"] options:NSDataBase64DecodingIgnoreUnknownCharacters];
[object_bullet setValue:bulletData forKey:#"bullet"];
[object_bullet setValue:response[#"groupId"] forKey:#"group_id"];
// set group
NSManagedObject *object = [NSEntityDescription insertNewObjectForEntityForName:#"Group"
inManagedObjectContext:self.context];
[object setValue:object_bullet forKey:#"relatedBullet"];
// NSDate *createDate = [dateFormatter dateFromString:response[#"createDate"]];
// [object setValue:createDate forKey:#"group_created_date"];
NSNumber *createDate = response[#"createDate"];
[object setValue:[NSDate dateWithTimeIntervalSince1970:[createDate doubleValue]] forKey:#"group_created_date"];
[object setValue:response[#"groupId"] forKey:#"group_id"];
[object setValue:[GeneralHelper getInteger:response[#"memberNumber"]] forKey:#"group_members_count"];
[object setValue:response[#"name"] forKey:#"group_name"];
[object setValue:[GeneralHelper getInteger:response[#"privacy"]] forKey:#"group_privacy"];
[object setValue:#1 forKey:#"group_status"];
[object setValue:response[#"imageName"] forKey:#"image_name"];
[object setValue:response[#"lastMessageId"] forKey:#"last_message_id"];
[object setValue:response[#"lastMessageText"] forKey:#"last_message_text"];
//
// NSDate *lastMessageDate = [dateFormatter dateFromString:response[#"lastMessageDate"]];
// [object setValue:lastMessageDate forKey:#"last_time"];
NSNumber *lastMessageDate = response[#"lastMessageDate"];
[object setValue:[NSDate dateWithTimeIntervalSince1970:[lastMessageDate doubleValue]] forKey:#"last_time"];
[object setValue:response[#"machineId"] forKey:#"machine_id"];
NSNumber *timestamp = response[#"timestamp"];
[object setValue:[NSDate dateWithTimeIntervalSince1970:[timestamp doubleValue]] forKey:#"timestamp"];
[object setValue:[GeneralHelper getInteger:response[#"unreadCount"]] forKey:#"unread_count"];
// }
}
NSError *errorInsert;
if (![self.context save:&errorInsert]) {
NSLog(#"Failed to save - error: %#", [errorInsert localizedDescription]);
}
[self.context rollback];
}
- (id)getMaxValue:(NSString *)entityName forProperty:(NSString *)propertyName {
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] initWithEntityName:entityName];
fetchRequest.fetchLimit = 1;
fetchRequest.sortDescriptors = #[[NSSortDescriptor sortDescriptorWithKey:propertyName ascending:NO]];
NSError *error = nil;
id maxValue = [self.context executeFetchRequest:fetchRequest error:&error].firstObject;
if (maxValue == nil) {
maxValue = #{propertyName : #0};
}
[self.context rollback];
return [maxValue valueForKey:propertyName];
}
- (id)getEntity:(NSString *)entityName AtValue:(NSString *)indexValue forProperty:(NSString *)propertyName {
NSFetchRequest *request = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:entityName inManagedObjectContext:self.context];
NSPredicate *predicate = [NSPredicate predicateWithFormat:#"%K == %#", propertyName, indexValue];
// NSLog(#"predicate: %#", predicate);
[request setEntity:entity];
[request setPredicate:predicate];
[request setFetchLimit:1];
NSError *error;
NSArray *results = [self.context executeFetchRequest:request error:&error];
NSManagedObject *object = [results firstObject];
// if (object) {
// // there is at least one object matching you predicate
// } else {
// // there is no object matching your predicate in the moc
// }
// NSLog(#"results member %#", results);
// [self.context rollback];
return object;
}
Then I use this general model in my UITableViewController.
Is there any better way to do this? Or my implementation is buggy and useless?
It think this is a nice approach, but it has some drawbacks.
An NSFetchedResultsController is responsible, as the name implies for controlling results that were fetched. If you add functionality like deleting and modifying things, you are making an object with too many responsibilities.
Imagine there is one viewController showing results in a tableview. It would only need the fetchedresultscontroller functionality, nothing more. If by tapping on a cell you go into a new view controller where edits are possible, you don't need a fetchedresultscontroller: you need a managedObject to edit. This editViewController could use a child context of the tableViewController so that on cancel, all edits are discarded by simply discarding the child context.
I would suggest to no subclass the NSFetchedResultsController, but instead implement a subclass of NSObject that you initialize with an NSNMagedObjectContext that can do the things that apply to the entire context. Like adding groups, removing all data etc.
Then use composition: this NSObject subclass can be a property of a viewController so the viewCOntroller has a way to do those things without having this operation being specific to this viewController.
In general, instead of subclassing, consider composition first. This is because transforming a small components of a composed object to an object that is a certain subclass is usually easier then the other way around.
Feel free to disagree :), but this is what I've learned from experience of working with many people for a long time on a very large codebase.
Hi good people I'm trying to prevent the freezing with
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_async(queue, ^{ CODE });
but I don't know how to use function.. I don't know where to put the managedObjectContext and how to use this dispatch_async my code is:
- (void)updateFacebookFriendsHighScore{
NSFetchRequest *requestche =[NSFetchRequest fetchRequestWithEntityName:#"Time"];
[requestche setReturnsObjectsAsFaults:NO];
NSPredicate *predicate=[NSPredicate predicateWithFormat:#"timeid==1"];
requestche.predicate=predicate;
NSArray *getIDTime = [self.managedObjectContext executeFetchRequest:requestche error:nil];
NSString *getTheTime = [[getIDTime valueForKey:#"time"] componentsJoinedByString:#""];
NSNumber *timeInInt = [NSNumber numberWithInteger: [getTheTime intValue]];
int timeFromDB = [timeInInt intValue];
timeFromDB = timeFromDB + 509;
int timeNow = [[NSDate date] timeIntervalSince1970];
if(timeNow > timeFromDB){
NSFetchRequest *updateHighScoreRequest = [NSFetchRequest fetchRequestWithEntityName:#"Friends"];
[updateHighScoreRequest setReturnsObjectsAsFaults:NO];
NSArray *friendsToUpdate = [self.managedObjectContext executeFetchRequest:updateHighScoreRequest error:nil];
for(NSArray *friendId in friendsToUpdate){
NSString *getFriendId = [friendId valueForKey:#"fbid"] ;
NSString *siteURL = [NSString stringWithFormat:#"http://www.example.com/example.php?fbid=%#", getFriendId];
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:siteURL]];
[NSURLConnection sendAsynchronousRequest:urlRequest queue:[[NSOperationQueue alloc] init] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
NSString *resultsFromDB = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSNumber *theScoreForUpdate = [NSNumber numberWithInt:[resultsFromDB intValue]];
NSFetchRequest *updateTheHighScoreRequest = [NSFetchRequest fetchRequestWithEntityName:#"Friends"];
NSPredicate *updateTheHighScorePredicate = [NSPredicate predicateWithFormat:#"fbid==%#",getFriendId];
updateTheHighScoreRequest.predicate=updateTheHighScorePredicate;
Friends *setScore = [[self.managedObjectContext executeFetchRequest:updateTheHighScoreRequest error:nil] lastObject];
NSLog(#"%#", setScore);
[setScore setValue:theScoreForUpdate forKey:#"score"];
[self.managedObjectContext save:nil];
data = nil;
resultsFromDB = nil;
theScoreForUpdate = nil;
setScore = nil;
}];
updateHighScoreRequest = nil;
}
}
requestche = nil;
}
This code gets the time from database and update the highscore after 509 seconds from the CD result and when I run this request my app freeze ( DEADLOCK ).
I am from Bulgaria and I'm trying to learn Objective C. Here we don't have schools for this our country is very bad in all instance and Bulgaria is last in Europe Union...
Can some serious and good person help me with my code or explane how works everything in Objective C or only help me with this ?
Try this code. To keep the application as simple as possible, never take the Core data code out of the main thread i.e. any thing related to self.managedObjectContext such as save or executing fetch requests. It is because Core data is not thread safe and you will have to device a strategy to handle that. I am assuming that your application is straight forward and you don't need such a strategy. So, please try to keep it as simple as possible and always perform the core data operations (save, execute) on main thread. dispatch_async(dispatch_get_main_queue(), ^{ code }); will execute it on main thread.
-(void) updateFacebookFriendsHighScore
{
dispatch_async(dispatch_get_main_queue(), ^{
NSFetchRequest *requestche =[NSFetchRequest fetchRequestWithEntityName:#"Time"];
[requestche setReturnsObjectsAsFaults:NO];
NSPredicate *predicate=[NSPredicate predicateWithFormat:#"timeid==1"];
requestche.predicate=predicate;
NSArray *getIDTime = [self.managedObjectContext executeFetchRequest:requestche error:nil];
NSString *getTheTime = [[getIDTime valueForKey:#"time"] componentsJoinedByString:#""];
NSNumber *timeInInt = [NSNumber numberWithInteger: [getTheTime intValue]];
int timeFromDB = [timeInInt intValue];
timeFromDB = timeFromDB + 509;
int timeNow = [[NSDate date] timeIntervalSince1970];
if(timeNow > timeFromDB){
NSFetchRequest *updateHighScoreRequest = [NSFetchRequest fetchRequestWithEntityName:#"Friends"];
[updateHighScoreRequest setReturnsObjectsAsFaults:NO];
NSArray *friendsToUpdate = [self.managedObjectContext executeFetchRequest:updateHighScoreRequest error:nil];
for(NSArray *friendId in friendsToUpdate){
NSString *getFriendId = [friendId valueForKey:#"fbid"] ;
NSString *siteURL = [NSString stringWithFormat:#"http://www.example.com/example.php?fbid=%#", getFriendId];
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:siteURL]];
[NSURLConnection sendAsynchronousRequest:urlRequest queue:[[NSOperationQueue alloc] init] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
NSString *resultsFromDB = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSNumber *theScoreForUpdate = [NSNumber numberWithInt:[resultsFromDB intValue]];
NSFetchRequest *updateTheHighScoreRequest = [NSFetchRequest fetchRequestWithEntityName:#"Friends"];
NSPredicate *updateTheHighScorePredicate = [NSPredicate predicateWithFormat:#"fbid==%#",getFriendId];
updateTheHighScoreRequest.predicate=updateTheHighScorePredicate;
Friends *setScore = [[self.managedObjectContext executeFetchRequest:updateTheHighScoreRequest error:nil] lastObject];
NSLog(#"%#", setScore);
[setScore setValue:theScoreForUpdate forKey:#"score"];
[self.managedObjectContext save:nil];
data = nil;
resultsFromDB = nil;
theScoreForUpdate = nil;
setScore = nil;
}];
updateHighScoreRequest = nil;
}
}
requestche = nil;
});
}
I have a method that saves a bunch of UIImages as JPGs and my app is crashing, I believe due to memory not being released. I'm using UIImageJPEGRepresentation to save the images, and I'm wrapping it in an autorelease pool but it doesn't seem to be working.
for (Images *anImage in images) {
NSAutoreleasePool* p = [[NSAutoreleasePool alloc] init];
NSString *fileName = [NSString stringWithFormat:#"%#-%i-%i-%i.jpg", appDelegate.currentUserName, appDelegate.reportId, aDefect.defectId, i];
[UIImageJPEGRepresentation(anImage.image, 1) writeToFile:localImagePath atomically:NO];
[p drain];
i++;
}
When I run the code above, the analyser shows me that more and more memory is being used, and it eventually crashes. If I remove the line - [UIImageJPEGRepresentation(anImage.image, 1) writeToFile:localImagePath atomically:NO]; it works fine.
FYI the loop iterates through an array of NSManagedObjects.
Any help would be appreciated! Thanks
Here is the new code as per suggestion -
- (void) convertImages {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSEntityDescription *entity = [NSEntityDescription entityForName:#"Report" inManagedObjectContext:context];
NSPredicate *predicate = [NSPredicate
predicateWithFormat:#"status != %#", #"Leads"];
[fetchRequest setEntity:entity];
[fetchRequest setPredicate:predicate];
[fetchRequest setIncludesPropertyValues:NO];
NSArray *fetchedObjects = [context executeFetchRequest:fetchRequest error:&error];
for (Report *aReport in fetchedObjects) {
appDelegate.reportId = [aReport.reportId intValue];
NSArray *defects = [self getAllItemDefects];
for (ItemDefect *anItemDefect in defects) {
NSArray *defectImages = [self getImages:[anItemDefect.itemDefectId intValue] isMainImage:NO];
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0ul);
dispatch_async(queue, ^(void) {
int i = 0;
for (Images *anImage in defectImages) {
NSString *fileName = [NSString stringWithFormat:#"%#-%i-%i-%i.jpg", appDelegate.currentUserName, [aReport.reportId intValue], [anItemDefect.itemDefectId intValue], i];
NSString *localImagePath = [documentsDirectory stringByAppendingPathComponent:fileName];
[UIImageJPEGRepresentation(anImage.image, 1) writeToFile:localImagePath atomically:NO];
i++;
}
dispatch_async(dispatch_get_main_queue(), ^{
NSLog(#"FINISH: do eventual operations");
});
});
}
}
}
That gives me this error -
If I load the defectImages array within the dispatch block, it just does nothing. Thanks for your help.
Edit - As CoreData is not thread safe, I've declared new FetchRequests and ObjectContexts to fix that problem. I'm no longer getting the bad access error, but I'm back to running out of memory. I've simplified the code for you, but its producing a memory leak in this state -
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0ul);
dispatch_async(queue, ^(void) {
NSFetchRequest *backgroundFetchRequest = [[NSFetchRequest alloc] init];
NSManagedObjectContext *backgroundContext = [[[NSManagedObjectContext alloc] init] autorelease];
[backgroundContext setPersistentStoreCoordinator:persistentStoreCoordinator];
NSEntityDescription *entity = [NSEntityDescription entityForName:#"Images" inManagedObjectContext:backgroundContext];
NSPredicate *predicate = [NSPredicate
predicateWithFormat:#"itemDefectId=%i AND reportId=%i AND isMainImage=%i", itemDefectId, appDelegate.reportId, NO];
[backgroundFetchRequest setEntity:entity];
[backgroundFetchRequest setIncludesPropertyValues:NO];
[backgroundFetchRequest setPredicate:predicate];
NSArray *defectImages = [backgroundContext executeFetchRequest:backgroundFetchRequest error:&error];
NSMutableArray *images = [[NSMutableArray alloc] init];
for (Images *anImage in defectImages) {
[images addObject:anImage.image];
}
[images release];
dispatch_async(dispatch_get_main_queue(), ^{
NSLog(#"FINISH: do eventual operations");
});
});
This isn't the good solution, use GCD:
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0ul);
for (int i=0; i<[images count]; i++) {
dispatch_async(queue, ^(void) {
Images *anImage = (Images *)[images objectAtIndex:i];
NSString *fileName = [NSString stringWithFormat:#"%#-%i-%i-%i.jpg", appDelegate.currentUserName, appDelegate.reportId, aDefect.defectId, i];
[UIImageJPEGRepresentation(anImage.image, 1) writeToFile:localImagePath atomically:NO];
dispatch_async(dispatch_get_main_queue(), ^{
NSLog(#"FINISH to write image %d", i);
});
});
}
Moreover, in your code, filename is ever the same, it doesn't change and isn't used. Your code is partial?
However, use dispatcher for async process.
I have a NSManagedObjectModel with an entity, "Project". I get all the projects to show them in a UITableView with NSFetchedResult controller. Now if the JSON has new Projects I insert them and if the JSON has updated items I update the items in the Core Data context.
So, my problem is when I get a JSON with less items than the context. I have thought about two ways to delete items in my context. One way is Delete all the context and save it again with the new items. The other way is create an array with all the items in the context and check it with the items in the JSON by id and if there is not one item, then remove it.
I have the idea, but I don't know which is the best way. I have thought also in a backgroundContext.
I use this method right now with out deleting methods:
#pragma mark - Project List service
- (void)getProjectListWithCpompletionBlock:(CompletionBlock)completionBlock{
NSMutableURLRequest *request = [self requestWithMethod:#"GET" path:kAPIProjectList parameters:nil];
[request setTimeoutInterval:kTimeOutRequest];
AFJSONRequestOperation *requestOperation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
NSDictionary *projects = [JSON valueForKey:kTagProjects];
for (NSDictionary *projectDic in projects) {
Project *pro = [Project getProjectWithId: [projectDic objectForKey:kTagProjectId] ];
if (pro) {
[Project updateProjectWithDictionary:projectDic];
NSLog(#"update %# ",[projectDic objectForKey:kTagProjectId]);
} else {
[Project createProjectWithDictionary: projectDic];
NSLog(#"create %# ",[projectDic objectForKey:kTagProjectId]);
}
}
[ypCoreDataManager saveContext];
if (completionBlock) {
completionBlock(NO, nil);
}
}
failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *aError, id JSON) {
NSLog(#"%# Failure JSNON Error%#", NSStringFromSelector(_cmd), aError);
if (completionBlock) {
completionBlock(YES, aError);
}
}];
[self enqueueHTTPRequestOperation:requestOperation];
}
Project+Helper is my project category and here it is the code.
+ (Project *)createProjectWithDictionary:(NSDictionary *)dic {
Project *project = nil;
project = [NSEntityDescription insertNewObjectForEntityForName:#"Project" inManagedObjectContext:mainContext];
project.projectId = [NSNumber numberWithInt:[[dic valueForKey:kTagProjectId] intValue]];
project.title = [[dic valueForKey:kTagProjectTitle]description];
project.estimatedPrice = [NSNumber numberWithInt:[[dic valueForKey:kTagProjectEstimatedPrice] floatValue]];
NSMutableArray *tags = [[NSMutableArray alloc] init];
tags = [dic objectForKey:kTagProjectsTags];
NSMutableSet *tagSet = [[NSMutableSet alloc]init];
for (NSDictionary * tagDic in tags){
NSString *tagName = [tagDic objectForKey:kTagProjectTagName];
Tag *tag = [Tag insertTagName:tagName inManagedObjectContext:mainContext];
[tagSet addObject:tag];
}
[project addTags:tagSet];
return project;
}
// Return project by id
+ (Project *)getProjectWithId:(NSString *) projectId {
Project *project = nil;
NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:#"Project"];
request.sortDescriptors = #[[NSSortDescriptor sortDescriptorWithKey:#"projectId" ascending:YES]];
request.predicate = [NSPredicate predicateWithFormat:#"projectId = %#", [projectId description]];
// Execute the fetch
NSError *error = nil;
NSArray *matches = [mainContext executeFetchRequest:request error:&error];
if (!matches || ([matches count] > 1)) { // nil means fetch failed; more than one impossible (unique!)
// handle error
} else { // found the Project, just return it from the list of matches (which there will only be one of)
project = [matches lastObject];
}
return project;
}
// Update project
+ (Project *)updateProjectWithDictionary:(NSDictionary *)dic {
Project *project = nil;
// Build a fetch request to see if we can find this Project in the database.
NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:#"Project"];
request.sortDescriptors = #[[NSSortDescriptor sortDescriptorWithKey:#"title" ascending:YES]];
request.predicate = [NSPredicate predicateWithFormat:#"projectId = %#", [dic[kTagProjectId]description]];
// Execute the fetch
NSError *error = nil;
NSArray *matches = [mainContext executeFetchRequest:request error:&error];
// Check what happened in the fetch
if (!matches || ([matches count] > 1)) { // nil means fetch failed; more than one impossible (unique!)
// handle error
} else {
project = [matches lastObject];
project.projectId = [NSNumber numberWithInt:[[dic valueForKey:kTagProjectId] intValue]];
project.title = [[dic valueForKey:kTagProjectTitle]description];
project.estimatedPrice = [NSNumber numberWithInt:[[dic valueForKey:kTagProjectEstimatedPrice] floatValue]];
NSMutableArray *tags = [[NSMutableArray alloc] init];
tags = [dic objectForKey:kTagProjectsTags];
NSMutableSet *tagSet = [[NSMutableSet alloc]init];
for (NSDictionary * tagDic in tags){
NSString *tagName = [tagDic objectForKey:kTagProjectTagName];
Tag *tag = [Tag insertTagName:tagName inManagedObjectContext:mainContext];
[tagSet addObject:tag];
}
[project addTags:tagSet];
}
return project;
}
You have to add this method in your Project's category and in your code after you add the new item call this method where you pass you array objects living in the Core Data and it remove all the objects more that you haven't in the array
+(void)removeExpiredProjectBy:(NSMutableArray *)ProjectLiving inContext:(NSManagedObjectContext *)context{
NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:#"Project"];
if (projectLiving.count) {
request.predicate = [NSPredicate predicateWithFormat:#"NOT (projectId IN %#)", [projectLiving copy]];
NSError *error = nil;
NSArray *matches = [context executeFetchRequest:request error:&error];
if (matches.count != 0) {
for (Project *pro in matches) {
[context deleteObject:pro];
}
}
}
}
As you process the JSON you can build a list of all of the ids that have been added / updated. Then, after that is complete you can create a fetch request with a predicate which finds all items where NOT (id IN %#) and supply the list of ids. This will return you only the items that need to be deleted.
Or, from an efficient API point of view, the server should give you a deletion list because it doesn't need to confirm items that haven't changed if you send it a 'last request date'...
My app parses information from a Rails app using JSON. I'm looking for a way to load the JSON asynchronously, but I'm having trouble getting my code to work with examples I have found because of the complexity of my code. What do I have to do to make my JSON load asynchronously? Thanks.
- (void)viewDidLoad
{
[super viewDidLoad];
NSURL *upcomingReleaseURL = [NSURL URLWithString:#"http://obscure-lake-7450.herokuapp.com/upcoming.json"];
NSData *jsonData = [NSData dataWithContentsOfURL:upcomingReleaseURL];
NSError *error = nil;
NSDictionary *dataDictionary = [NSJSONSerialization JSONObjectWithData:jsonData options:0 error:&error];
NSArray *upcomingReleasesArray = [dataDictionary objectForKey:#"upcoming_releases"];
//This is the dateFormatter we'll need to parse the release dates
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"];
NSTimeZone *est = [NSTimeZone timeZoneWithAbbreviation:#"EST"];
[dateFormatter setTimeZone:est];
[dateFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:#"en_US"]]; //A bit of an overkill to avoid bugs on different locales
//Temp array where we'll store the unsorted bucket dates
NSMutableArray *unsortedReleaseWeek = [[NSMutableArray alloc] init];
NSMutableDictionary *tmpDict = [[NSMutableDictionary alloc] init];
for (NSDictionary *upcomingReleaseDictionary in upcomingReleasesArray) {
//We find the release date from the string
NSDate *releaseDate = [dateFormatter dateFromString:[upcomingReleaseDictionary objectForKey:#"release_date"]];
//We create a new date that ignores everything that is not the actual day (ignoring stuff like the time of the day)
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *components =
[gregorian components:(NSDayCalendarUnit | NSMonthCalendarUnit | NSYearCalendarUnit) fromDate:releaseDate];
//This will represent our releases "bucket"
NSDate *bucket = [gregorian dateFromComponents:components];
//We get the existing objects in the bucket and update it with the latest addition
NSMutableArray *releasesInBucket = [tmpDict objectForKey:bucket];
if (!releasesInBucket){
releasesInBucket = [NSMutableArray array];
[unsortedReleaseWeek addObject:bucket];
}
UpcomingRelease *upcomingRelease = [UpcomingRelease upcomingReleaseWithName:[upcomingReleaseDictionary objectForKey:#"release_name"]];
upcomingRelease.release_date = [upcomingReleaseDictionary objectForKey:#"release_date"];
upcomingRelease.release_price = [upcomingReleaseDictionary objectForKey:#"release_price"];
upcomingRelease.release_colorway = [upcomingReleaseDictionary objectForKey:#"release_colorway"];
upcomingRelease.release_date = [upcomingReleaseDictionary objectForKey:#"release_date"];
upcomingRelease.thumb = [upcomingReleaseDictionary valueForKeyPath:#"thumb"];
upcomingRelease.images = [upcomingReleaseDictionary objectForKey:#"images"];
[releasesInBucket addObject:upcomingRelease];
[tmpDict setObject:releasesInBucket forKey:bucket];
}
[unsortedReleaseWeek sortUsingComparator:^NSComparisonResult(id obj1, id obj2) {
NSDate* date1 = obj1;
NSDate* date2 = obj2;
//This will sort the dates in ascending order (earlier dates first)
return [date1 compare:date2];
//Use [date2 compare:date1] if you want an descending order
}];
self.releaseWeekDictionary = [NSDictionary dictionaryWithDictionary:tmpDict];
self.releaseWeek = [NSArray arrayWithArray:unsortedReleaseWeek];
}
One simple approach is to use NSURLConnection's convenient class method sendAsynchronousRequest:queue:error.
The following code snippet is an example how to load a JSON from a server, and where the completion handler executes on a background thread which parses the JSON. It also performs all recommended error checking:
NSURL* url = [NSURL URLWithString:#"http://example.com"];
NSMutableURLRequest* urlRequest = [NSMutableURLRequest requestWithURL:url];
[urlRequest addValue:#"application/json" forHTTPHeaderField:#"Accept"];
NSOperationQueue* queue = [[NSOperationQueue alloc] init];
[NSURLConnection sendAsynchronousRequest:urlRequest
queue:queue
completionHandler:^(NSURLResponse* response,
NSData* data,
NSError* error)
{
if (data) {
NSHTTPURLResponse* httpResponse = (NSHTTPURLResponse*)response;
// check status code and possibly MIME type (which shall start with "application/json"):
NSRange range = [response.MIMEType rangeOfString:#"application/json"];
if (httpResponse.statusCode == 200 /* OK */ && range.length != 0) {
NSError* error;
id jsonObject = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error];
if (jsonObject) {
dispatch_async(dispatch_get_main_queue(), ^{
// self.model = jsonObject;
NSLog(#"jsonObject: %#", jsonObject);
});
} else {
dispatch_async(dispatch_get_main_queue(), ^{
//[self handleError:error];
NSLog(#"ERROR: %#", error);
});
}
}
else {
// status code indicates error, or didn't receive type of data requested
NSString* desc = [[NSString alloc] initWithFormat:#"HTTP Request failed with status code: %d (%#)",
(int)(httpResponse.statusCode),
[NSHTTPURLResponse localizedStringForStatusCode:httpResponse.statusCode]];
NSError* error = [NSError errorWithDomain:#"HTTP Request"
code:-1000
userInfo:#{NSLocalizedDescriptionKey: desc}];
dispatch_async(dispatch_get_main_queue(), ^{
//[self handleError:error]; // execute on main thread!
NSLog(#"ERROR: %#", error);
});
}
}
else {
// request failed - error contains info about the failure
dispatch_async(dispatch_get_main_queue(), ^{
//[self handleError:error]; // execute on main thread!
NSLog(#"ERROR: %#", error);
});
}
}];
Although, it appears somewhat elaborate, IMO this is a minimalistic and still naïve approach. Among other disadvantages, the main issues are:
it lacks the possibility to cancel the request, and
there is no way to handle more sophisticated authentication.
A more sophisticated approach needs to utilize NSURLConnection delegates. Usually, third party libraries do implement it in this manner, encapsulating the a NSURLConnection request and other relevant state info into a subclass of NSOperation. You may start with your own implementation, for example using this code as a template.
If you just want to get this only json data, you do not need to set up a lot of things.
use the code below. Create jsonParse method which gets a NSData Object.
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0ul);
dispatch_async(queue, ^{
NSData *data = [[NSData alloc]initWithContentsOfURL:[NSURL URLWithString:#"http://obscure-lake-7450.herokuapp.com/upcoming.json"]];
dispatch_sync(dispatch_get_main_queue(), ^{
[self jsonParse:data];
});
});
Download your data async as in this answer: Object-c/iOS :How to use ASynchronous to get a data from URL?
Then run it through the json parser.
To generically run code in a background thread you can use this method:
dispatch_async( dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
// Code here is run on a background thread
dispatch_async( dispatch_get_main_queue(), ^{
// Code here is run on the main thread (the UI thread) after your code above has completed so you can update UI after the JSON call has completed if you need to.
});
});
But remember that Apple does not allow you to update UI elements in a background thread. Also, they do not allow you to spawn more threads from a background thread, it must be done from the main thread.
NSString *urlstr=#"http://itunes.apple.com/in/rss/topsongs/limit=25/json";
NSMutableURLRequest *request=[[NSMutableURLRequest alloc]initWithURL:[NSURL URLWithString:urlstr]];
[NSURLConnection sendAsynchronousRequest:request
queue:[[NSOperationQueue alloc] init]
completionHandler:^(NSURLResponse* response,
NSData* data, NSError* error)
{
NSError *myError = nil;
NSDictionary *dic1 = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:&myError];
if (myError ==nil) {
NSDictionary*feed =[dic1 objectForKey:#"feed"];
NSArray*arrayofentry =[feed objectForKey:#"entry"];
for(NSDictionary *dic2 in arrayofentry) {
requestReply=[dic2 objectForKey:#"title"];
[arr1 addObject:requestReply];
}
[self.table reloadData];
}
}];
Try this code:
NSURL * inkURL = [NSURL URLWithString:#"your url"];
NSURLRequest * request = [[NSURLRequest alloc]initWithURL:inkURL cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:10.0];
NSOperationQueue * queue = [[NSOperationQueue alloc]init];
[NSURLConnection sendAsynchronousRequest:request queue:queue completionHandler:^(NSURLResponse * response, NSData * data, NSError * error) {
NSData * jsonData = [NSData dataWithContentsOfURL:inkURL];
NSDictionary * dataDictionary = [NSJSONSerialization JSONObjectWithData:jsonData options:0 error:&error];
self.inkArray = [dataDictionary objectForKey:#"users"];
}];