1

I just drag the Data.csv file to the application folder in the Navigator panel, I am trying to set the correct path of the file into the app.

The code below I used for the simulator and works perfect, but to run in the device I changed to the second block of code, then I got this errors:

Data[399:157757] CFURLCopyResourcePropertyForKey failed because it was passed an URL which has no scheme
Error Domain=NSCocoaErrorDomain Code=256 "The file “Documents” couldn’t be opened."
UserInfo={NSURL=/var/mobile/Containers/Data/Application/C7756542-6922-4C6F-A98E-C6F407B2063E/Documents}

//code to show the path in the simulator:
         guard let remoteURL = NSURL(string: "/Users/mbp/Library/Developer/CoreSimulator/Devices/7F25FC7C-F2B2-464E-85B4-A2B96DB83F17/data/Containers/Bundle/Application/F285940D-7776-4EE2-83A1-D54DD3411E0E/Data.app/Data.csv") else {
            return
        }

Block to run the app in the device:

  func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {


        let sourcePath = NSBundle.mainBundle().pathForResource(“Data”, ofType: "csv")
        print(sourcePath)

        let filename = "Data.csv"
        let documentsPath = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0]
        let destinationPath = documentsPath + "/" + filename

        do {
            try NSFileManager().copyItemAtPath(sourcePath!, toPath: destinationPath)
        } catch _ {
        }

Try to load the file
        let fetchRequest: NSFetchRequest = NSFetchRequest(entityName: "DataEntity")
        fetchRequest.fetchLimit = 1
        do {
            let result = try managedObjectContext.executeFetchRequest(fetchRequest)
            if result.count == 0 {
                 preloadData()
            }
        } catch let error as NSError {
            print("Error: \(error.domain)")
        }


    func preloadData () {

        guard let remoteURL = NSURL(string:NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0]) else {
            return
        }
        }
vadian
  • 274,689
  • 30
  • 353
  • 361
Manolo
  • 469
  • 6
  • 20
  • @LeoDabus ok what should I write? I don't know the path for the file in the device. How can I get it? Thanks for the reply :) – Manolo Feb 03 '16 at 13:39
  • check this post http://stackoverflow.com/a/34548888/2303865 – Leo Dabus Feb 03 '16 at 13:40
  • 1
    Your path at the simulator it is not the same at your device. You are hard coding your path. You should use URLByAppendingPathComponent to compose your url – Leo Dabus Feb 03 '16 at 13:41
  • and check this http://stackoverflow.com/a/27693945/2303865 – Leo Dabus Feb 03 '16 at 13:46
  • @Manolo you can also add scheme directly ex. guard let remoteURL = NSURL(string: "file:///Users/mbp/Library/Developer/CoreSimulator/Devices/7F25FC7C-F2B2-464E-85B4-A2B96DB83F17/data/Containers/Bundle/Application/F285940D-7776-4EE2-83A1-D54DD3411E0E/Data.app/Data.csv") else { return } ;however, I also prefer the way of URLByAppendingPathComponent as well. – Allen Feb 03 '16 at 14:54
  • @Allen agree, but this is the path for the simulator, what I am looking for is the path for the device. I want to make sure the app is installing with the data bundle. When I open the device the data is not on it. :( – Manolo Feb 03 '16 at 16:01
  • @Manolo Yeah, so NSURL is the best bet in my opinion. Please take my answer for references. Cheers! – Allen Feb 03 '16 at 16:30

1 Answers1

6

Process file path via NSURL can avoid the mismatch between device and simulator.


        let srcURL = NSBundle.mainBundle().URLForResource("Data", withExtension: "csv")!
        let documentsPath = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0]
        var toURL = NSURL(string: "file://\(documentsPath)")!
        toURL = toURL.URLByAppendingPathComponent(srcURL.lastPathComponent!)
        do {
            try NSFileManager().copyItemAtURL(srcURL, toURL: toURL)
            self.preloadData(toURL)
        } catch let error as NSError {
            print(error.localizedDescription)
        }

        func preloadData(toURL: NSURL) {
            print("=== Success and print toURL ===")
            print(toURL)
        }
Allen
  • 1,714
  • 17
  • 19
  • 1
    get this error: 2016-02-03 19:34:00.397 data[39577:782986] CFURLCopyResourcePropertyForKey failed because it was passed an URL which has no scheme The file couldn’t be opened because the specified URL type isn’t supported. – Manolo Feb 03 '16 at 16:35
  • @Manolo Oops, just verified with a newly creating project, please check my revised answer. – Allen Feb 03 '16 at 16:40
  • I think the last func preloadData should have a path of the file Am I wrong? – Manolo Feb 03 '16 at 16:48
  • @Manolo Yeah, I think so~ – Allen Feb 03 '16 at 16:51
  • so what pat I should set there? :( – Manolo Feb 03 '16 at 16:52
  • @Manolo Can be more straightforward since the copy process is successful. I just revised my answer. – Allen Feb 03 '16 at 16:56
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/102498/discussion-between-manolo-and-allen). – Manolo Feb 03 '16 at 16:56
  • Tks Allen, it is using the file bundled, but still takes long time to load the database. :( – Manolo Feb 04 '16 at 09:19
  • @Manolo A trick is to be called shipping a pre-populated core data. I suggest you can have some researches about it. – Allen Feb 04 '16 at 09:45
  • yes I am into this right now. Thanks for your time Allen. – Manolo Feb 04 '16 at 09:47