2016-12-24 85 views
0

我在local2上使用local2,它在鉻和iphone上本地工作,但似乎無法在Android設備上工作。Ionic 2 @ ionic/storage在Android設備上無法正常工作

我真的不想重寫和使用Sqlite,因爲我的數據比表結構更適合json結構。任何線索?

CODE

import { Injectable } from '@angular/core'; 
import { Storage } from '@ionic/storage'; 
import { Observable } from 'rxjs/Rx'; 

import { Customer } from '../pages/customer/customer.model' 

@Injectable() 
export class StorageService { 
    constructor(private _storage: Storage) { } 

    setSelectedCustomer(customer: Customer) { 
     return Observable.fromPromise(this._storage.set('selectedCustomer', customer)); 
    } 
} 

版本:

"@ionic/storage": "^1.1.7", 
"ionic-angular": "2.0.0-rc.3", 
"ionic-native": "2.2.3", 

感謝

+0

你能否提供一些代碼片段並解釋你已經嘗試過什麼? –

+0

總是有一些沒有答案的問題,只知道如何點擊投票。 – Milad

+0

謝謝@Milad我在想,但沒有意思。 –

回答

2

它不是用一個好主意localStorage保存importa nt數據在混合應用程序中。有關Android上的問題以及iOS上的某些問題的報告太多。一般來說,這是由不正確的數據類型處理引起的。舉例來說,你試圖保存一些對象,但在Chrome中,Android可能會以不同的方式進行序列化。因此,最好是做手工,如:

this._storage.set('selectedCustomer', JSON.stringify(customer)) 

,然後使用JSON.parsethis article提到的仍然是問題從JSON字符串解析的對象。因此,最好是使用一些更強大的存儲用戶數據(如SQLite的)

另外,如果你不想改變從JSON到關係表結構,你可以嘗試LokiJS

+0

對於其他NoSQL替代品,您可以查看類似Couchbase或PouchDB的內容。這裏有一篇關於Couchbase + Ionic 2的文章:https://blog.couchbase.com/2016/december/data-synchronization-with-couchbase-in-ionic-2-hybrid-mobile-apps – Hod

0

我這樣使用它和它的做工精細

this._storage.set('selectedCustomer', JSON.stringify(customer)); 

this._storage.get('selectedCustomer').then((selectedCustomer) => { 
    let json_selectedCustomer = JSON.parse(selectedCustomer); 
    }); 
+0

您正在使用承諾,並將轉換爲來自Promise的Observable。我不認爲這是問題。 –

+0

我想提一下,當您使用存儲作爲鍵/值時,您必須將該值作爲字符串發送。 –

相關問題