0

I've made a class to pickle and unpickle data from a dictionary. Its fairly standard: it tried to open the file, if the file's not present it creates it and it offers methods to read from the dictionary and write to the dictionary.

However, when packaged as an APK using buildozer (I made sure to delete the data file before compiling), sometimes data is randomly lost on the device, and if you update the app through the android updater all data is lost. I don't know where the data file is, but I'm presuming it's kept in the APK. Can anyone offer a solution to this?

I'm using Python 2 and Kivy 1.10 and the built-in pickle module. Thanks in advance

The code for my pickler is as below:

import pickle 
class Data(object):
  def __init__(self,fname):
    try: 
      f = open(fname,'r')
      p = pickle.load(f)
      f.close() 
    except IOError:
      f = open(fname,'w')
      pickle.dump({'highscore':0},f)
      f.close() p = {'highscore':0}
    self.fname = fname
    self.data = p 
  def rewrite(self,key,value):
    new = self.data
    new[key] = value
    f = open(self.fname,'w')
    pickle.dump(new,f)
    f.close() 
    self.data = new
  def grab(self,key):
    with open(self.fname,'r') as f:
      self.data = pickle.load(f)  
      return self.data[key]
  • After reading this post: https://stackoverflow.com/questions/26289325/save-app-data-in-kivy-on-android , I have a few questions. Did you search for a text file on your device SD card and memory? Are you trying to store images? – hridayns Jun 29 '17 at 08:58
  • @code_byter its saving as a binary file, and searching doesnt show it. I'm just storing a dictionary –  Jun 29 '17 at 10:05
  • Are you opening the pickle file in the right "mode"? "write" mode while saving the data into the file and "read" mode when trying to access data? Also, you mentioned that you packaged the app using buildozer, so I'm looking to see if that made some changes that cause the data loss. From your question, what I understand is that you tested the pickling and unpickling of the data without packaging it first, and it worked? Is that right? – hridayns Jun 29 '17 at 10:12
  • @code_byter I'm using the right modes, it writes/reads correctly. The highscore saving works on both PC and android, but like I said sometimes I think the data disappears, and on update all data gets lost –  Jun 29 '17 at 10:15
  • Okay, so sometimes, a part of the data disappears? and when you update the data stored (eg. Highscores), its all lost? Did you try the code for updating the data, without packaging it? – hridayns Jun 29 '17 at 10:16
  • @code_byter yes, and it works without being packaged. its on a gist, i can add it to the question? –  Jun 29 '17 at 10:21
  • Yes, please do. – hridayns Jun 29 '17 at 10:23
  • @code_byter im doing this from my phone so it might not be perfectly formatted but i added it above –  Jun 29 '17 at 10:30
  • okay, so just to get this right, the problem is that, all these pickle functions work great before the packaging. And after packaging, your `rewrite` function seems to lose all the data. (I'm assuming update means to rewrite the data). – hridayns Jun 29 '17 at 10:35
  • I noticed that none of the modes specify `rb` or `wb`, to write it into a binary file. I am going through this link http://www.diveintopython3.net/serializing.html to figure out if your code needs to be changed for the post packaging part. – hridayns Jun 29 '17 at 10:45
  • @code_byter update as in install an update to the apk, like as in if i rolled out an update over google play. and yeah, i forgot about the `rb wb` bit... but this is using python 2 and im not sure why it works without in python 2 –  Jun 29 '17 at 11:32
  • 1
    Please take a look at this https://stackoverflow.com/questions/31224756/android-app-updating-without-losing-data - It seems to me that Pickling is basically serializing your data, which does not seem to persist after an update. Have you considered trying `SharedPreferences` or `SQLite`. Also, take a look at http://www.benfrederickson.com/dont-pickle-your-data/ – hridayns Jun 30 '17 at 04:21
  • @code_byter thanks, ill look at integrating that into kivy –  Jun 30 '17 at 05:55
  • Sure no problem. Hope it works! :) – hridayns Jun 30 '17 at 05:56

0 Answers0