Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Unity Project Build to iPhone not retaining data between instances.

Discussion in 'Editor & General Support' started by XezbethTheLiar, Apr 29, 2020.

  1. XezbethTheLiar

    XezbethTheLiar

    Joined:
    Apr 29, 2020
    Posts:
    6
    UPDATE****FIXED*****
    It turns out that I was using the Awake() function where it should have been a Start() function.
    *********

    I am having an issue with my save-data on my app. I followed this tutorial;

    After doing everything in the video, the app works great... until I build it to iPhone via Xcode, that is.

    The specific area I am having trouble with is the to-do list portion of my app. Each item is stored in a list of type string, but then when I do the binary formatting, I convert that list to an array of strings, as I thought this was the problem initially. But even still, it still doesn't work.

    Anyone know what could be the problem?
     
    Last edited: Apr 30, 2020
  2. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    I'd suggest posting the relevant code. If you expect people tto sit through a 20 minute video to get to the code, you're probably going to be disappointed.
     
  3. XezbethTheLiar

    XezbethTheLiar

    Joined:
    Apr 29, 2020
    Posts:
    6
    ToDoListSaveSystem.cs
    Code (CSharp):
    1. using UnityEngine;
    2. using System.IO;
    3. using System.Runtime.Serialization.Formatters.Binary; //Access binary formatter
    4.  
    5. public static class todoListSaveSystem
    6. {
    7.     public static void SaveToDoList(list ListImport)
    8.     {
    9.         BinaryFormatter formatter = new BinaryFormatter();
    10.         string path = Application.persistentDataPath + "/user.fun";
    11.  
    12.         FileStream stream = new FileStream(path, FileMode.Create);
    13.  
    14.         listData ListData = new listData(ListImport);
    15.  
    16.         formatter.Serialize(stream, ListData);
    17.         stream.Close();
    18.     }
    19.  
    20.     public static listData LoadListData()
    21.     {
    22.         string path = Application.persistentDataPath + "/user.fun";
    23.         if (File.Exists(path))
    24.         {
    25.             BinaryFormatter formatter = new BinaryFormatter();
    26.             FileStream stream = new FileStream(path, FileMode.Open);
    27.  
    28.             listData data = formatter.Deserialize(stream) as listData; //Decodes and assigns the stored data
    29.             stream.Close();
    30.  
    31.             return data;
    32.         }
    33.         else
    34.         {
    35.             Debug.LogError("Save file not found in " + path);
    36.             return null;
    37.         }
    38.     }
    39. }
    40.  
    list.cs
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using System.Linq;
    5.  
    6. public class list : MonoBehaviour
    7. {
    8.     public List<string> taskName;
    9.  
    10.     public testTodoDriver driver;
    11.  
    12.     public void SaveList()
    13.     {
    14.         Debug.Log("SAVE LIST");
    15.         todoListSaveSystem.SaveToDoList(this);
    16.     }
    17.  
    18.     public void LoadList()
    19.     {
    20.         Debug.Log("LOAD LIST");
    21.         listData loadedData = todoListSaveSystem.LoadListData();
    22.  
    23.         taskName = loadedData.taskNameList.ToList();
    24.  
    25.         foreach (string task in taskName)
    26.         {
    27.             driver.InstantiateList(task);
    28.             driver.isEmpty = true;
    29.         }
    30.     }
    listData.cs
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.  
    6. [System.Serializable]
    7. public class listData
    8. {
    9.     //public List<string> taskName = new List<string>();
    10.     private int size;
    11.     public string[] taskNameList;
    12.  
    13.     public listData(list ListImport)
    14.     {
    15.         taskNameList = ListImport.taskName.ToArray();
    16.     }
    17. }
     
  4. XezbethTheLiar

    XezbethTheLiar

    Joined:
    Apr 29, 2020
    Posts:
    6
    The code posted above is what I am working with. A quick TL;DR of it's function;

    toDoListSaveSystem.cs is the driver class for the actual saving. It uses a binary formatter to convert the array located in the list.cs class into binary and then into a file with the extension type ".fun".

    list.cs is the main class for the list. It contains a List of type string. Whenever a To-Do list item is added in the app, a new list item is created.

    listData.cs is the class that, upon instantiation with a related list.cs object, converts the List<string> into an array of strings. This array is then passed into toDoListSaveSystem.cs where it is formatted into binary and stored into the file.

    My problem EXACTLY is that this works perfectly fine on my computer, however, when I build it to Xcode and then to my iPhone, everything seems to work BUT the to-do list system.

    My first thought was whether or not a List<string> exists on every OS, hence the reason behind converting to an array before encoding.

    Any help is appreciated. If you need further clarification, please ask. Thanks!
     
  5. XezbethTheLiar

    XezbethTheLiar

    Joined:
    Apr 29, 2020
    Posts:
    6
    I uploaded the code and some explanatory text. Sorry I didn't do that sooner.