LOCATION BEACON - Remaining storage space

Hi!
I’m trying to find out how much storage space remains in a Location Beacon because I want to store JSON content inside with an image (encoded in base64) and I can’t store all the string of the JSON.

The problem is that from a certain size of the image, it isn’t displayed and this limit depends on the length of the rest (Example: description). In the code below, I have an image and a description. If in the description I remove the sequence of ‘A’, the image is displayed. If I leave this sequence of characters, the image isn’t displayed and in this case, if I put a smaller image, it is displayed.

So I’m looking for if there is a method to get the remaining storage space to see if it’s my JSON that is too long or if it just comes from my code.

My code :

    NSString * nameJSON = @"Tour de la Lanterne";
    NSString * imageJSON = [self encodeToBase64String:[UIImage imageNamed: @"t4s.jpg"]];
    NSString * descriptionJSON = @"La tour de la Lanterne (xve siècle) est, avec la tour Saint-Nicolas et la tour de la Chaîne, l'une des trois tours du front de mer de La Rochelle, vestiges des fortifications médiévales qui protégeaient le port. Elle est également connue sous les noms de tour du Garrot, tour des Prêtres et tour des Quatre Sergents. Elle a été classée monument historique le 17 février 1879. Une ordonnance du corps de la ville datant de 1209 mentionne déjà l'existence d'une « tour de la Chaîne », n'ayant aucun rapport avec l'actuelle tour de la Chaîne, mais ayant la même fonction. En effet, celle-ci barrait alors l'entrée du port primitif de La Rochelle situé sur le ruisseau du Lafond, et fut par la suite dénommée « tour du Garrot » du nom de l'appareil de levage qui servait à désarmer les vaisseaux avant que ceux-ci puissent entrer dans le port. Le capitaine de tour était désigné comme étant le « désarmeur des nefs ». Selon Claude Masse, le nouvel ouvrage, commencé en 1445, incorpora par chemisage cette ancienne tour. Il ne fut achevé que 23 ans plus tard (1468), grâce aux deniers personnels du maire de l'époque, Jean Mérichon. À l'origine, elle formait l'angle sud-ouest de l'enceinte médiévale et sa tourelle à lanterne servait de phare et d'amer. Elle se situait à cette époque au bord de l'eau. Conservé lors du rasement des fortifications en 1629, elle fut ensuite intégrée dans la nouvelle enceinte de 1689. De 1900 à 1914, on entreprend la restauration de la tour. Suivant les projets de Juste Lisch, puis sous la direction d'Albert Ballu, la restauration lui redonne son aspect médiéval. Lors des rénovations de 2014-2015, deux gargouilles ont été recréées à l'effigie des dessinateurs et caricaturistes Cabu et Wolinski. AAAAAAAAAAAAAAAAAAAAAAAAAAA ... AAAAAAAAAAAAA";
    
    NSLog(@"%@", imageJSON);
    
    NSString * jsonString = @"{\n\"name\" : \"";
    jsonString = [jsonString stringByAppendingString:nameJSON];
    jsonString = [jsonString stringByAppendingString:@"\",\n\"image\" : \""];
    jsonString = [jsonString stringByAppendingString:imageJSON];
    jsonString = [jsonString stringByAppendingString:@"\",\n\"description\" : \""];
    jsonString = [jsonString stringByAppendingString:descriptionJSON];
    jsonString = [jsonString stringByAppendingString:@"\"\n}"];

The JSON NSString displayed in logs:

"name" : "Tour de la Lanterne",
"image" : "
iVBORw0KGgoAAAANSUhEUgAAAL4 ... Zz+pLG3HRRa3YpAL6AARwaV7dhf0EK //I checked, the base64 is not complete and so the JSON content is not complete too

I checked the sizes of each NSString and I have the following sizes:

Size of name : 19
Size of Base64 : 117916
Size of description : 3750
Size of JSON String : 121734

When adding with the number of characters in addition to the three variables (name, image and description), we get the size of the JSON String.

Thank you for your help!

We don’t currently have a method to check the remaining storage size, but this should help:

  • there’s exactly 122880 bytes of storage available in the current beacons
  • whatever dictionary you pass to saveStorageDictionary, we currently serialize it to JSON and save into the beacon storage

Knowing that, you should be able to calculate the serialized-size of your data, and subtract it from 122880 to get the free space in bytes.

Example:

NSDictionary *myData = // ...
NSData *myDataAsJson = [NSJSONSerialization dataWithJSONObject:myData 
                                                       options:kNilOptions
                                                         error:nil];
if (myDataAsJson.length <= 122880) {
    NSLog(@"it fits!");
    // note that we save `myData` here!
    // the SDK will serialize it to JSON for you
    [self.beacon.storage saveStorageDictionary:myData completion: // ...
} else {
    NSLog(@"it's too big!");
}

Note the comment about saving myData, not myDataAsJson! If you pass JSON data to saveStorageDictionary, you essentially end up serializing data to JSON twice (once yourself, and then the SDK will do that again), which wastes space.

Disclaimer: the fact that we’re using JSON underneath is an implementation detail, and so we reserve the right to change this at any time, e.g., should we decide we want to switch to something more efficient.

Thank you for your answer heypiotr!

Indeed, with what you have just given me, the size of my NSData is too big. I understand now why I can to store only small images.
Just a quick question: where did you find this number of bytes: 122880? Because on the site of Estimote, it is specified that the EPPROM can contain up to 1Mb?

image

We reserve a little bit of this space for our own logs/storage, hence 122,880 and not 125,000.

(note it’s one 1 Mb = 1 mega-bit, not 1 MB = 1 mega-byte)

Oh! Ok.
Thank you very much for your help! :slight_smile: