Memory Pressure Issue in read Metadata and remove duplicate - ios

-(void) objectParsed_ListAllMedia:(NSDictionary *)dictionary
{
#try {
self.viewLoading.hidden=1;
[self.arrGaalleryMediaName removeAllObjects];
[self.arrMediaNames removeAllObjects];
if(self.arrOnlyServerImages == nil){
self.arrOnlyServerImages = [[NSMutableArray alloc] init];
}
if([self.arrOnlyServerImages count] >0){
[self.arrOnlyServerImages removeAllObjects];
}
if (dictionary==nil) {
[self.gridCollectionView reloadData];
return;
}
// Filter Array for Audio file
NSPredicate *predicate = [NSPredicate predicateWithFormat: #"type != 'audio' "];
self.arrOnlyServerImages = [NSMutableArray arrayWithArray:[[dictionary objectForKey:#"objects"] filteredArrayUsingPredicate:predicate]];
// Remove duplicate Start //Read Meta Data and Duplicate from Download, Duplicate from upload START
dispatch_queue_t backgroundQueue = dispatch_queue_create("com.memreas.myqueue", 0);
dispatch_async(backgroundQueue, ^{
NSMutableArray *arr = [NSMutableArray array];
NSMutableIndexSet * indexSet = [NSMutableIndexSet indexSet];
for (int i=0; self.assetAry.count>i; i++) {
ALAsset *result =self.assetAry[i];
ALAssetRepresentation *imageRep = [result defaultRepresentation];
NSDictionary * customMetaDic = [imageRep metadata][(NSString*)kCGImagePropertyIPTCDictionary];
if (customMetaDic) {
[self.arrMediaNames addObject:customMetaDic[(NSString*)kCGImagePropertyIPTCObjectName]?customMetaDic[(NSString*)kCGImagePropertyIPTCObjectName]:#""];
}else{
[self.arrMediaNames addObject:#""];
}
[self.arrGaalleryMediaName addObject:[self getFileNameWithExtensionFromPath:imageRep.url]];
}
for (int i=0; self.arrOnlyServerImages.count>i; i++) {
NSMutableDictionary* obj = self.arrOnlyServerImages[i];
NSMutableDictionary * dic2 = [NSMutableDictionary dictionaryWithDictionary:obj];
BOOL isArrMedia =[self.arrMediaNames containsObject:dic2[#"media_name"]];
BOOL isGallery =[self.arrGaalleryMediaName containsObject:dic2[#"media_name"]];
if (isArrMedia||isGallery) {
dic2[#"isDownloaded"] = [NSNumber numberWithBool:YES];
[indexSet addIndex: isArrMedia?[self.arrMediaNames indexOfObject:dic2[#"media_name"]] :[self.arrGaalleryMediaName indexOfObject:dic2[#"media_name"]]];
}else{
dic2[#"isDownloaded"] = [NSNumber numberWithBool:NO];
}
[arr addObject:dic2];
}
dispatch_async(dispatch_get_main_queue(), ^{
#try {
self.arrOnlyServerImages = arr;
[self.assetAry removeObjectsAtIndexes:indexSet];
[self.gridCollectionView reloadData];
}
#catch (NSException *exception) {
NSLog(#"%#",exception);
[self.gridCollectionView reloadData];
}
});
});
// Remove duplicate END //Read Meta Data and Duplicate from Download, Duplicate from upload END
[self.gridCollectionView reloadData];
[self.gridView.collectionView reloadData];
[self.location performSelector:#selector(stopActivity) withObject:nil afterDelay:2];
}
#catch (NSException *exception) {
NSLog(#"%#",exception);
}
}
I have issue with my code, While I run this code it generates memory pressure issue and crash the app.
Functionality is:
I load all the images from server and local assets and match each other with file name and remove duplicate images from list, So it will visible only once.
Any one have solution so please help.
thanks in advance.

You're accessing memory-intensive things (ALAssetRepresentations) in a tight loop. In these cases a local autoreleasepool can help ARC to keep your memory use down.
Inside the loop where you pass through self.assetAry, wrap everything in an autoreleasepool like so:
#autoreleasepool {
AlAsset *asset = ...
...
// Rest of your code
}

Related

Realm+JSON: EXC BAD ACCESS when calling createOrUpdateInRealm:withJSONArray: second time

+ (NSDictionary *)mc_inboundMapping {
static NSMutableDictionary *mappingForClassName = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
mappingForClassName = [NSMutableDictionary dictionary];
});
#synchronized(mappingForClassName) {
NSDictionary *mapping = mappingForClassName[[self className]];//<----EXC BAD ACCESS here
if (!mapping) {
SEL selector = NSSelectorFromString(#"JSONInboundMappingDictionary");
if ([self respondsToSelector:selector]) {
mapping = MCValueFromInvocation(self, selector);
}
else {
mapping = [self mc_defaultInboundMapping];
}
mappingForClassName[[self className]] = mapping;
}
return mapping;
}
I'm using Realm+JSON with Realm. And got a problem with Realm+JSON.
When I attempt to call createOrUpdateInRealm:withJSONArray: multiple time in loop for multiple RLMObjects, the first RLMObject works OK, but the second RLMObject(no matter what class is) fails with EXC BAD ACCESS code1on static variable.
I think autorelease pool deallocate pointing NSMutableDictionary.Anybody got this issue? I'm using XCode 6.3.2 and Realm 0.96.3.
+ (NSArray *)createOrUpdateInRealm:(RLMRealm *)realm withJSONArray:(NSArray *)array {
NSInteger count = array.count;
NSMutableArray *result = [NSMutableArray array];
for (NSInteger index=0; index*kCreateBatchSize<count; index++) {
NSInteger size = MIN(kCreateBatchSize, count-index*kCreateBatchSize);
#autoreleasepool
{
for (NSInteger subIndex=0; subIndex<size; subIndex++) {
NSDictionary *dictionary = array[index*kCreateBatchSize+subIndex];
id object = [self createOrUpdateInRealm:realm withJSONDictionary:dictionary];
[result addObject:object];
}
}
}
return [result copy];
}
calling here...
[realm beginWriteTransaction];
if ([collection isKindOfClass:[NSDictionary class]])
{
for (NSString *key in [collection allKeys])
{
NSLog(#"Set Data == %#:%#", key, collection[key]);
id jsData = [collection[key] objectForKey:#"data"];
if ([key isEqualToString:STATIC_ERROR_MSGS])
{
[SErrorMsgs createOrUpdateInRealm:realm withJSONArray:jsData];
}
else if ([key isEqualToString:STATIC_EPISODE])
{
[SEpisode createOrUpdateInRealm:realm withJSONArray:jsData];
}
else if ([key isEqualToString:STATIC_INGAME_TUTORIAL])
{
[SIngameTutorial createOrUpdateInRealm:realm withJSONArray:jsData];
} //more cases.........edited

Changes in NSManagedObject does not save in to CoreData

I have two methods to save and load NSManagedObject (updated to original):
-(void) saveToCoreData: (TeamManagedObject *)teamSet{
RKLogConfigureByName("RestKit/ObjectMapping", RKLogLevelDebug);
if(!teamSet) {
self.teamSet = [NSEntityDescription insertNewObjectForEntityForName:#"TeamManagedObject" inManagedObjectContext:self.managedObjectContext];
} else {
self.teamSet = teamSet;
}
[self.teamSettings enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
[self.teamSet setValue:obj forKey:key];
}];
NSError *saveError = nil;
[self.managedObjectContext save:&saveError];
}
-(id)load:(TeamManagedObject *)managedObject {
unsigned int count = 0;
objc_property_t *properties = class_copyPropertyList([TeamManagedObject class], &count);
for (int i = 0; i < count; i++) {
NSString *key = [NSString stringWithUTF8String:property_getName(properties[i])];
NSString *value = [managedObject valueForKey:key];
if (value) {
[self setObject:value forKey:key];
}
}
self.teamSet = managedObject;
free(properties);
return self;
}
1) call method [self save:nil] - object creates and saves to CoreData correctly. (value is correct after having app restarted)
2) restarting app and calling load method - it was loaded correctly
3) calling save [self save:object] method with loaded object - it looks like saved, but only before app was restarted... after app restarting, object have an old value...
Where is the mistake?
Thanks!

ios - possible memory leaks with nsmutablearray not deleted?

I'm new to iOS development and in my app I'm seeing some strange memory usage behavior.
I'm getting objects from server in such setupDataForPage method:
- (void)setupDataForPage:(int)page actionType:(NSString *)type success:(void (^)())callback
{
__weak MyTableViewController *weakSelf = self;
// clearing image cache because feed contains a lot of images
[[SDImageCache sharedImageCache] clearMemory];
[[SDImageCache sharedImageCache] clearDisk];
MyHTTPClient *API = [MyHTTPClient new];
[API feedFor:page success:^(AFHTTPRequestOperation *operation, id data) {
NSArray *data = [data objectForKey:#"data"];
if ([data count] > 0) {
// remove all objects to refresh with new ones
if ([type isEqualToString:#"pullToRefresh"]) {
[weakSelf.models removeAllObjects];
}
// populate data
NSMutableArray *result = [NSMutableArray new];
for (NSDictionary *modelData in data) {
MyModel *model = [[MyModel alloc] initWithDictionary:modelData];
[result addObject:model];
}
[weakSelf.models addObjectsFromArray:result];
[weakSelf.tableView reloadData];
}
callback();
} failure:nil];
}
it is used in viewDidLoad while getting initial request and also for pull refresh and infinite scrolling:
- (void)viewDidLoad {
[super viewDidLoad];
__block int page = 1;
__weak MyTableViewController *weakSelf = self;
// initial load
[self setupDataForPage:page actionType:#"initial" success:^{ page += 1; }];
// pull to refresh
[self.tableView addPullToRefreshWithActionHandler:^{
[weakSelf setupDataForPage:1 actionType:#"pullToRefresh" success:^{
[weakSelf.tableView.pullToRefreshView stopAnimating];
}];
}];
// infinite scrolling
[self.tableView addInfiniteScrollingWithActionHandler:^{
[weakSelf setupItemsForPage:page actionType:#"infiniteScroll" success:^{
page += 1;
[weakSelf.tableView.infiniteScrollingView stopAnimating];
}];
}];
}
I noticed that even after pull to refresh action which returns the same data (and I'm just removing all models and add them once more) my app's memory usage grows from nearly 19mb to 24mb..
I would like someone more experienced to look at this piece of code to determine whether it contains some possible memory leaks.. Should I somehow delete NSMutableArray *result variable after assigning it to models array?
Thanks!
First of all, use #autoreleasepool here:
#autoreleasepool {
NSArray *data = [data objectForKey:#"data"];
if ([data count] > 0) {
// remove all objects to refresh with new ones
if ([type isEqualToString:#"pullToRefresh"]) {
[weakSelf.models removeAllObjects];
}
// populate data
NSMutableArray *result = [NSMutableArray new];
for (NSDictionary *modelData in data) {
MyModel *model = [[MyModel alloc] initWithDictionary:modelData];
[result addObject:model];
}
[weakSelf.models addObjectsFromArray:result];
[weakSelf.tableView reloadData];
}
}
#autoreleasepool allows you to release every object allocated in that scope IMMEDIATELY.
This is perfect situation where use it ;)

Infinite loop traversing cloud storage file structure [Box api]

I'm trying to get the metadata from the box api and so I need to traverse the entire directory structure. Here is the code:
- (IBAction)ls {
// INITIALIZE folderID (will be nil only for the root folder)
if (self.folderID == nil)
{
self.folderID = BoxAPIFolderIDRoot;
self.folderName = #"Root";
}
// START TRAVERSING DIRECTORY TREE
[self traverseItemsWithFolderID:self.folderID name:self.folderName];
}
- (void)traverseItemsWithFolderID:(NSString *)folder name:(NSString *)namefolder
{
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
[self fetchFolderItemsWithFolderID:folder name:namefolder]; // <- List Files/dirs in directory namefolder
dispatch_async(dispatch_get_main_queue(), ^{
// Create box object "item" to traverse folderItemsArray
BoxItem * item = (BoxItem *)[self.folderItemsArray objectEnumerator];
for (item in self.folderItemsArray)
{
if([item.type isEqualToString:BoxAPIItemTypeFolder])
{
NSLog(#"SubFolder id %#", item.modelID);
NSLog(#"SubFolder name %#", item.name);
[self traverseItemsWithFolderID:item.modelID name:item.name];
}
}
});
});
}
and this last method:
- (void)fetchFolderItemsWithFolderID:(NSString *)innerFolder name:(NSString *)innerFolderName
{
BoxFoldersResourceManager *mgr = [BoxSDK sharedSDK].foldersManager;
BoxFoldersRequestBuilder *bldr = [[BoxFoldersRequestBuilder alloc] initWithQueryStringParameters:#{ #"fields" : #"name,type,id,etag,size,modified_at,hash" }];
[mgr folderItemsWithID:innerFolder requestBuilder:bldr success: ^(BoxCollection *collection)
{
self.folderItemsArray = [NSMutableArray array];
for (NSUInteger i = 0; i < collection.numberOfEntries; i++)
{
[self.folderItemsArray addObject:[collection modelAtIndex:i]];
NSLog(#"FOLDER NAME %#", innerFolderName);
BoxItem *item = (BoxItem *)[self.folderItemsArray objectAtIndex:i];
NSLog(#"Here The list of Files/Directories:");
NSLog(#"TypE %#", item.type); // [file/directory]
NSLog(#"NAME %#", item.name); // filename
NSLog(#"ID %#", item.ETag); // directory level [0 = root]
NSLog(#"ID %#", item.modelID); // metadata unique identifier [FolderID !]
NSLog(#"SIZE %#", item.size); // in bytes
NSLog(#"Modified Time %#", item.modifiedAt); // in UTC format
NSLog(#"HASH %lu", (unsigned long)item.hash); // hash
}
self.totalCount = [collection.totalCount integerValue];
NSLog(#" Number of files/dirs in Level %ld", (long)self.totalCount);
}
The final result is that the code start an infinite loop of requests and don't understand why, as it should stop when there are no more directories. Any help on identifying the issue is much appreciated.
Thanks !
You should avoid using the property self.folderItemsArray since you have a recursive method, try this:
- (NSArray*)fetchFolderItemsWithFolderID:(NSString *)innerFolder name:(NSString *)innerFolderName
{
...
// This is a new line.
NSMutableArray* folderItemsArray = [NSMutableArray array];
[mgr folderItemsWithID:innerFolder requestBuilder:bldr success: ^(BoxCollection *collection)
{
//self.folderItemsArray = [NSMutableArray array]; remove this line.
for (NSUInteger i = 0; i < collection.numberOfEntries; i++)
{
[folderItemsArray addObject:[collection modelAtIndex:i]]; // remove the self here.
...
}
return folderItemsArray;
}
And update this method like this:
- (void)traverseItemsWithFolderID:(NSString *)folder name:(NSString *)namefolder
{
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSArray* items = [self fetchFolderItemsWithFolderID:folder name:namefolder]; // <- List Files/dirs in directory namefolder
dispatch_async(dispatch_get_main_queue(), ^{
// Create box object "item" to traverse folderItemsArray
//BoxItem * item = (BoxItem *)[self.folderItemsArray objectEnumerator]; why do you need this line.
for (BoxItem *item in items)
{
if([item.type isEqualToString:BoxAPIItemTypeFolder])
{
NSLog(#"SubFolder id %#", item.modelID);
NSLog(#"SubFolder name %#", item.name);
[self traverseItemsWithFolderID:item.modelID name:item.name];
}
}
});
});
}

Why mi property do not save the value when I use SudzC?

I am using SudzC to get webservices, that webservice give me data, I tried to save the data in a property, but when I use to fill a tableview the property don't have any data. I use the debugger to view the property.
This es my handler
- (void) ConsultarUnidadesOrganizacionalesPorEmpresaHandler: (id) value {
// Handle errors
if([value isKindOfClass:[NSError class]]) {
NSLog(#"%#", value);
return;
}
// Handle faults
if([value isKindOfClass:[SoapFault class]]) {
NSLog(#"%#", value);
return;
}
// Do something with the NSMutableArray* result
NSMutableArray *result = (NSMutableArray*)value;
NSMutableArray *unidadOrganizacional = [[NSMutableArray alloc] init];
self.myData = [NSMutableArray array];
for (int i = 0; i < [result count]; i++)
{
EWSUnidadNegocio *empresa = [[EWSUnidadNegocio alloc] init];
empresa = [result objectAtIndex:i];
[unidadOrganizacional addObject:[empresa Descripcion]];
}
self.myData = unidadOrganizacional;
}
And this is the part where I use the web service
- (void)viewDidLoad
{
// Do any additional setup after loading the view from its nib.
EWSEmpresaWebServiceSvc *service = [[EWSEmpresaWebServiceSvc alloc]init];
[service ConsultarUnidadesOrganizacionalesPorEmpresa:self action:#selector(ConsultarUnidadesOrganizacionalesPorEmpresaHandler:) EmpresaId:self.empresaID];
[super viewDidLoad];
}
And the tableview is empty. Why does this happen? How can I use the data and fill my tableview?
Try to change your code this way :
self.myData = [[NSMutableArray alloc] init]autorelease];
// your loop
[sel setMyData:unidadOrganizacional];
try loading it in a dictionary:
dict = [resp objectForKey:#"YourKey"];
if( ( dict == nil ) || ![dict isKindOfClass:[NSDictionary class]] ) {
NSLog( #"WARNING: %#", [dict description]);
return;
}
empID = [[dict objectForKey:#"empresaID"]copy];
NSLog(#"Your Value: %#", empID);
I found the answer, I just have to put
[_myTableView reloadData]
at the end of the method.

Resources