Is there a way to get programatically the size of a core data objects (in bytes )?
Asked
Active
Viewed 1,111 times
0
-
`[nsDataObj length]` gives the bytes. – Anoop Vaidya Apr 10 '13 at 11:16
-
2@Anoop: `NSData` and Core Data (`NSManagedObject`) are entirely different things. – Ole Begemann Apr 10 '13 at 11:19
-
That's why i used `nsDataObj`. You can convert MangedObj to NSData and find. Hope [ManagedObject to Data](http://stackoverflow.com/questions/2357544/how-to-convert-an-nsarray-of-nsmanagedobjects-to-nsdata) will come handy. – Anoop Vaidya Apr 10 '13 at 11:23
-
This should do the trick but what about the difference between the archived object size and the NSManagedObject object size ? – Alexandre Apr 10 '13 at 11:33
-
Sorry not much experience in this. – Anoop Vaidya Apr 10 '13 at 11:35
-
Alexandre, please don't use salutations and thanks when asking the question. See [this](http://meta.stackexchange.com/questions/2950/should-hi-thanks-taglines-and-salutations-be-removed-from-posts) – Krishnabhadra Apr 10 '13 at 11:37
2 Answers
1
Here, existed SO thread to convert NSManagedObjectID into NSData
NSURL * url = [[YOUROBJECT objectID] URIRepresentation];
NSData * urlData = [NSKeyedArchiver archivedDataWithRootObject:url];
NSLog(@"Data Length :%d",[urlData length]);
-
I'm sorry I though that this worked but it gives me the size of the reference and not the size of the saved object. – Alexandre Apr 12 '13 at 15:46
0
you can use below code for calculating both array and sum of individual NSManagedObject
- (void) calculateSize {
NSFetchRequest *request = [[NSFetchRequest alloc] initWithEntityName:@"Event"];
NSArray *data = [self.managedObjectContext executeFetchRequest:request error:nil];
NSLog(@"Size of %@: %zd", NSStringFromClass([data class]), malloc_size((__bridge const void *) data));
int totalSize = 0;
for(NSManagedObject *object in data)
{
totalSize += malloc_size((__bridge const void *) object);
}
NSLog(@"Managed Object Size total:%d", totalSize);
}

Chandan
- 500
- 3
- 10