What will be the size(memory)of NSMutableArray? - iphone

I am parsing json data,and i store them into an NSMutableArray.
For each data,parameters are as the following format,
{
"id":"6",
"username":"biju",
"password":"biju123",
"usertype":"2",
"first_name":"biju",
"last_name":null,
"email":"b#b.com",
"blocked":"N",
"created":"2012-11-02 16:03:19",
"image":"http:\/\/192.168.1.254\/eatthos\/assets\/upload\/users\/1351852399_thumb.jpg","thumb_image":"1351852399_thumb.jpg",
"menu_image":"0",
"thumb_menu_image":"0",
"city":"njdfh",
"favorite_food":"kafh",
"favorite_restaurant":"kafdhj",
"phone_number":"0",
"description":"0",
"token":"Dwx0DG",
"fb_unique_id":null,
"account_type":"normal",
"twitter_id":null,
"followers":"5",
"follow":"N"
}
i am parsing about 100K of data,what will be the size(memory) of that array ?
Will it be a memory issue if i use it for an iphone app ?

You can try to find out size of your array with the following piece of code :
size_t total;
id obj;
for (obj in array)
{
total += class_getInstanceSize([obj class]);
}
NSLog(#"Array size : %ld",total); //This will return you size in bytes
you need to do : #import <objc/runtime.h>

The array itself won't take that much space -- it's just a bunch of pointers. An array with tens of thousands of items can still only be a few dozen kilobytes. The space taken by the objects that the array contains is likely to be much more significant. But that is something only you can see -- there is no "size of an object in an array." It's like asking "How big is a ball?" It is possible that your data's space needs are problematic, and it's equally possible that there's no problem at all. As with many programming questions, I think the best answer is to try and see for yourself.

Why can not you download 100 records each time like an apps then are in app store
So, That the app will load quicker that before
If u want to see the flow of the memory you can go through this link http://mobileorchard.com/find-iphone-memory-leaks-a-leaks-tool-tutorial/

Related

Cache uitableview data in memory

I get an array (containing text) called titleArray from json which I populate a uitableview with. I would like to cache it in memory so that data for the screen loads once, it will not need to load again per session. I've never done this..
basically I have 2 methods:
- (void)requestFinishedWithResult:(NSDictionary*)result
- (void)requestFailed
depending on if I get anything from the server.
Somewhere in the - (void)requestFinishedWithResult:(NSDictionary*)result I'm thinking I need an additional array to store the titleArray data in...and then do something like this: ??
-(void)requestFailed
{
if (titleArray != nil)
{
storeArray = titleArray;
}
}
or something like that? but I really don't know how it should work. Any help is appreciated.
Depends on what kind of persistance you want. You can store it on the disk and then load it and have a mechanism to update that cached data. Or, you can just store on an ivar and retain it's value, this way you will have the data for the duration of the application.

sorting array containing arrays ios

I made a little program calculating gas consumption.
There is one view controller for each data entry necessary to calculate consumption in the final view controller.
So you enter the driven distance in the first controller, the gas fuelled up in the next controller. in the controller displaying the result, the current date is added, too.
All entries are stored in a plist file as strings for easier use with the tableviews later (except for the date).
Now I want to open this data in a summary view controller to populate a tableview.
Currently I have retrieve 3 arrays from the plist file (similar to the columns of a table):
array 1: NSDate
array 2: NSString representing a number
array 3: NSString representing a number
I know how to populate my tableview, but I cannot wrap my head around the sorting.
I tried changing the column approach into a table row based approach getting every single element from each of the arrays and adding them to a new array, containg each a date and two numbers (as strings).
I tried setting up sortdescriptors, I tried using selectors, but it seems I don't fully comprehend how to work it. Personally, I prefer examples, so I was looking for tutorials on this topic. The developer documentary didn't help that much.
I don't expect to get a complete example for this problem, but maybe some pointers on how to arrange my data and what best to use to sort the complete data array.
Thanks for any pointers or examples.
EDIT:
I finally got all "Table-lines" as "rows" into a dictionary. Now I can access the single columns by key for the column. And this also allows sorting the stringvalues in my array of dictionaries ascending or descending using a sortdescriptor.
This may not be the most elegant approach, but I think I now can do what I wanted.
Excuse the "Table" comparisons, but I find this easier to grasp.
If anyone has pointers to good array tutorials, I'd be more than thankful.
I'm not really clear on how you want to sort, but maybe you can adapt something from this (sorts an array of dictionaries each with a key #"row" with a value type NSNumber) -
First of all, make a comparison result function:
static NSComparisonResult compareRowNumbers(NSDictionary *dict1, NSDictionary *dict2, void * context) {
if ([[dict1 objectForKey:#"row"] intValue] < [[dict2 objectForKey:#"row"] intValue]) {return NSOrderedAscending;}
if ([[dict1 objectForKey:#"row"] intValue] > [[dict2 objectForKey:#"row"] intValue]) {return NSOrderedDescending;}
return NSOrderedSame;
}
Then you can use it like so -
NSMutableArray *myArray = [NSMutableArray array];
// populate your array with loads of stuff (in this case, NSDictionaries as decribed above)
// now sort it
[myArray sortUsingFunction:compareRowNumbers context:NULL];
...and your array is automagically sorted.

Get amount of memory used by app in iOS

I'm working on an upload app that splits files before upload. It splits the files to prevent being closed by iOS for using too much memory as some of the files can be rather large. It would be great if I could, instead of setting the max "chunk" size, set the max memory usage and determine the size using that.
Something like this
#define MAX_MEM_USAGE 20000000 //20MB
#define MIN_CHUNK_SIZE 5000 //5KB
-(void)uploadAsset:(ALAsset*)asset
{
long totalBytesRead = 0;
ALAssetRepresentation *representation = [asset defaultRepresentation];
while(totalBytesRead < [representation size])
{
long chunkSize = MAX_MEM_USAGE - [self getCurrentMemUsage];
chunkSize = min([representation size] - totalBytesRead,max(chunkSize,MIN_CHUNK_SIZE));//if I can't get 5KB without getting killed then I'm going to get killed
uint8_t *buffer = malloc(chunkSize);
//read file chunk in here, adding the result to totalBytesRead
//upload chunk here
}
}
Is essentially what I'm going for. I can't seem to find a way to get the current memory usage of my app specifically. I don't really care about the amount of system memory left.
The only way I've been able to think of is one I don't like much. Grab the amount of system memory on the first line of main in my app, then store it in a static variable in a globals class then the getCurrentMemUsage would go something like this
-(long)getCurrentMemUsage
{
long sysUsage = [self getSystemMemoryUsed];
return sysUsage - [Globals origSysUsage];
}
This has some serious drawbacks. The most obvious one to me is that another app might get killed in the middle of my upload, which could drop sysUsage lower than origSysUsage resulting in a negative number even if my app is using 10MB of memory which could result in my app using 40MB for a request rather than the maximum which is 20MB. I could always set it up so it clamps the value between MIN_CHUNK_SIZE and MAX_MEM_USAGE, but that would just be a workaround instead of an actual solution.
If there are any suggestions as to getting the amount of memory used by an app or even different methods for managing a dynamic chunk size I would appreciate either.
Now, as with any virtual memory operating system, getting the "memory used" is not very well defined and is notoriously difficult to define and calculate.
Fortunately, thanks to the virtual memory manager, your problem can be solved quite easily: the mmap() C function. Basically, it allows your app to virtually load the file into memory, treating it as if it were in RAM, but it is actually swapped in from storage as it is accessed, and swapped out when iOS is low on memory.
This function is really easy to use in iOS with the Cocoa APIs for it:
- (void) uploadMyFile:(NSString*)fileName {
NSData* fileData = [NSData dataWithContentsOfMappedFile:fileName];
// Work with the data as with any NSData* object. The iOS kernel
// will take care of loading the file as needed.
}

How to avoid image duplication in image gallery of iOS device?

I want to store the images to photo gallary. But if images are already available at photo gallary then how to distinguish for whether imgaes are already exists or not? I didnt find property like unique identifier or should i compare by taking data in the form of NSdata?
thanks..
You can maintain a dictionary of hashes for each the image in the photo gallery, and then show only additional images who do not hashes are not present in the dictionary
As a reminder, you can check for an object in a dictionary by doing:
if ([myDictionary objectForKey:variableStoringImageHash] == nil) {
//No such image present
}
else {
//image is present
}
For a bit about hashing an image, this might help:
iPhone: fast hash function for storing web images (url) as files (hashed filenames)
I am not sure if what eitan27 says will work so as an alternative I would say that your best hope is comparing NSData objects. But this as you can see will become very tedious as there will be n number of images in library and comparing each one for repetition does't make sense , still if you want to compare data you look at this answer which will give you a fraction of how much the data is matching.

How to display Summation in TableView in iPhone SDK?

Is it possible to display information in TableView as given in screenshot?
Here: 'MyFund' is sum of amounts of Rob, Mike and Sam Fund.
I am not sure how will I sum up the amounts and display in Table headers.
Please help!
Hmm. I suppose you could calculate the summation in sectionIndexTitlesForTableView as such:
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
{
// replace the following with however you'll calculate the sums
int sumSectionOne = 4;
int sumSectionTwo = 3;
return [NSArray arrayWithObjects:[NSString stringWithFormat:#"$%d",sumSectionOne], [NSString stringWithFormat:#"$%d",sumSectionTwo], nil];
}
but it does mean you'll be invoking reloadSections a lot on that table view. Moreover, this isn't entirely the intended purpose of UITableView (though admittedly a really easy way of doing this).
There is no sort cut for this you need to implement a good store data structre then a logic to fetch them.so its all about logic,see my approach.
You need to add this record in an array with dictionary form,means add fund with money in a dictionary with two different key says "fund" and "money"(any thing).
when you make section means in titleForHeaderInSection method, you need to implement the logic.you need to make loop upto array count and access the dictionaries and add all "money" values.
Now you need to implement.if you have face any problem in code then again ask.