How to save and load a RoomPlan scan to use as a "test" scan - swift

I'm working on a RoomPlan app. I've successfully managed to capture, save and load scans using NSKeyedArchiver. However, for apps run on the simulator or on devices without a LiDAR Scanner, I do not have any scans saved on that device so I cannot test the functionality. I want to "fill" my app with a test scan or two to be able to test my app on those devices.
The core problem here is that I can't figure out how to "capture" objects in Swift and save them hardcoded in the app instead of in NSKeyedArchiver, which is local to every device.

You are doing this:
RoomScan --(via NSKeyedArchiver/Encodable)--> Data --(save)--> UserDefaults
UserDefaults --(read)--> Data --(via NSUnarchiver/Decodable)--> RoomScan
So, what I suggest:
Scan a room, then convert it as Data, save it if needed.
Intercept that Data either by forcing a reading, or before saving into UserDefaults.
Choose the solution you find easier/quicker for you for the "intercept":
Convert the Data into HexString (see How to convert Data to hex string in swift), print it into Console, copy the output or save maybe that content into a Data file, and send it via AirDrop/Mail.
Create a new file into your project, and read it when needed (simulator, etc). Depending on the solution chosen since the writing might differ, the reading also might differ.

Related

How to store data localy in swift osx app?

I'm working on my first osx App and I want to save arrays of objects that I've created on a "project file" like you do in any program when using the "save" button.
I have been looking different ways to do it, Core Data, Document based app, but, I'm not sure how it works any of them to choose the best option for my app.
I want to save arrays of objects that have inside more arrays and other strings and doubles that contain information of the app that the user added. ( It can be a lot of data ) So for that reason I think Userdefault is not a good idea.
I also want to make the app able to open one of this projects saved.
So, could you help me to find which is the best way to do it?
I can suggest you to read iOS FileSystem Overview.
You must use the Data container to locate your local files. For this, you have to locate the NSDocumentDirectory with the NSUserDomainMask (call to NSSearchPathForDirectoriesInDomains) and use standard NSFileManager class.

Restkit seed core-data with objects from a local file on launch

I'm working on an app, in which I need to seed core-data with some local data stored in a file and I'm using Restkit.
Right now, I have stored the data in JSON format in a text file, and on launch I use the "RKManagedObjectImporter" to map the JSON to objects. Since the data is huge, it takes a lot of time (at least 30 seconds) just to map the data from JSON to Core-data objects.
I obviously cannot make the user wait 30 seconds on the first launch, and the data is required as soon as the app launches. So what alternative do I have here ? Is there a way to create mapped objects and store them in a file ?
Any help will be appreciated!
You can create a Core Data .sqlite file and distribute it with the app instead of a JSON file. Then, when the app launches you can check if the user has an existing store file and, if not, copy the default file from the bundle into the desired location.
RestKit also offers importing support using a .sqlite source file, see here.

Saving, reading, exchanging own file format?

any recommendations for saving four strings into an own file format that is read by the application and can be shared?
In my app, you will be able to enter some text in some boxes and the app shows a view with an background image and those strings. Now, I am already able to save this as a picture, but I actually want to save it to an own file format so that you can save different files that can be modified afterwards as well or even exchanged via email and opened from another iphone with the app.
Now, I wrote the code for accessing the documents folder of the app, as well as saving and deleting. Thing is, i dont know how to store those strings in a file (perhaps in a xml?) and read them easily afterwards from my application.
For the exchanging part, I found that link which would be a nice feature indeed: http://iosdevelopertips.com/cocoa/launching-your-own-application-via-a-custom-url-scheme.html
Parsing xml seems not that difficult (never done it before): http://ipad.about.com/od/iPad-App-Dev/a/How-To-Parse-Xml-Files-In-Xcode-Objective-C.htm
If it's only a small bit of infomation then the easiest way to store your data in a file would be using a plist - there's a good tutorial here - http://www.icodeblog.com/2009/02/14/loading-data-from-plist-files/
In addition to the plist, you could also do the following approaches:
1) simplest - open a file in your documents directory, write the 4 strings (using whatever delimiter/end of string marker is useful - carriage return?) and overwrite them each time through. (i.e. it's your file, you can format it how you like)
2) mildly harder - use Apple's NSArchive to pack and unpack the strings.
3) possible overkill - store them directly in a SQLite database
4) major overkill - store them in CoreData.
Of course, the "overkill" options also provide you with extra features which may be of use if your app functionality extends beyond what you've outlined.
By sharing, I would think that simple copy and paste might be enough, but there's also sending it via email, or tripping another app's URL scheme to make it open it and sending the strings as part of the URL. You'd have to know that the other app would be able to interpret your strings as part of the URL, so you might have to write it yourself.
Okay guys I found that very nice method in the NSString documentation:
–writeToFile:atomically:encoding:error:
I think I am gonna separate my strings by /n and save them to a .txt. When I am gonna read the file afterwards, i am getting the string, divide it to substrings. That will do it, I guess.
One last question: Is it possible to replace the content of my file so that I won't need to create a new file every time i want to change something?
Thanks!

iPhone app that reads .csv file

Can an iOS app read a file that has been placed on that device e.g. a CSV file.
So the idea is when you're at home you create the file with a list of items or you get emailed it etc on your PC and then you place this file (in the correct format) on the device which reads it.
Is this possible?
You can read CSV data but "place the file on the device" would have to involve a web service of some sort. There is no way to add data to an application without getting in a from a web server. But if you have a web server that can host the csv file then opening and reading it is fairly simple.
this covers most o it
Import csv data (SDK iphone)
NSString stringWithContentsOfURL:encoding:error
will take care of the rest
There is a string feature that will take each value and place it into an array:
NSArray *anArray = [yourString componentsSeparatedByString:#","];
You can parse CSV on an iOS device.
You can place a data file on an iOS device.
Beware though of CSV format details. Just splitting a string on "," is a surefire way to get failures and random errors. Have a look at http://cocoawithlove.com/2009/11/writing-parser-using-nsscanner-csv.html for a good example on how to handle CSV data.
I've personally used the example from cocoa with love to create an operation that downloads a CSV from a http location, parses and processes it and then stores the results in a Core-Data store.
But you could just as well get the CSV file on your device by using an NSURLConnection. Beware though: NSURLDownload is unavailable on iOS. Also downloading data to the device is not recommended. Apple recommends processing the data and storing the results or just getting the data as you need it.

Prefilling Core Data for a read-only application

i am working on an application that displays read-only data i am shipping.
it is more of a book.
It is easy with SQLite but i am not satisfied with the performance and trying to use Core Data.
The issue is with pre-filling Core Data is that it is a hard process.
My question is:
Is it possible to build an assistant iphone application (for me to use) which uses the same data model for pre-filling.
and then take the populated .xcdatamodel file and use it in my original application?
I hope this makes sense :)
Adham
I believe what you're asking is whether you can create a CoreData database upfront and copy it to the iPhone. Is that correct?
This article will help. Here's a quote:
I thus suggest the following five-step process:
Create your data in a comma-separated file, typically placing each row of data (an entity) in a row of the file and separating different columns (its attributes) by commas.
Write a standalone program and copy in your .xcdatamodel file from your main project.
Write code in your new program that parses your comma-separated file and inserts the information into a Core Data persistent store that should be identical to the persistent store in your main project.
Run the program in the Simulator
Copy your data from the Simulator's documents directory into your actual project's bundle.
It's possible, I've done it. I made a desktop application to read from a CSV file using the code here:
http://www.mac-developer-network.com/columns/coredata/may2009/
I just had to alter the way the CSV part worked, and change the model.
I copied and pasted my model from the model builder into the iPhone model. (Clicked on the "grid" area, selected all, copied)
Then I took the sqlite database the desktop app produced (found it in Application Support, in the folder for this application) and put it into the resources folder
I made some code to copy the sqlite into the documents folder on the iPhone (if it wasn't already there) at startup, in the applicationDidLaunch method. It's possible that having it in the resources folder is no good. Even though you're using the database as read only, Core Data may want to write something to it. Not sure about this though..
I used the sqlite file in the documents folder in my Core Data set up.
The desktop and iPhone Core Data sqlite file seem to be exactly the same format. You can transfer one sqlite file to another application (iPhone to iPhone too) as long as they have the same data model. In another application, I used NSXMLParser to create the Core Data sqlite file, then transferred it to another app, both on iPhone using the Simulator.
Yeah, your data source can be whatever you want it to be. The other suggestions are good ones. Create a managed object model (.xcdatamodel) identical to what you want to use in your app. Read in the data from your file, create a new instance of your managed object and populate it from the file. Then save, and dive into the bundle in the iPhone Simulator and copy it over. This has the added bonus of being in exactly the format you need, with all the helpful metadata. Copy your object model and your managed object classes and you're good to go.
Note, though, if you really intend for it to be read-only, and you're using it at install, it will be installed in your finished app's bundle (under Applications/{SIGNATURE}/Myapp.app). If you intend to edit this database or allow a user to save to it, it's a better idea to copy it to the Applications/{SIGNATURE}/Documents directory where your user database lives.