objective-c - ios - 客户端在搜索时如何与后端交换数据?
怪我咯
怪我咯 2017-04-18 09:04:06
0
3
391

初学iOS,在做搜索功能的时候遇到了这个问题。
做的是一个商品类应用,在用户搜索商品时,目前打算把所有商品名一次性在初始化时获取,然后再搜索。但是如果商品数量很大,我觉得这样不现实。
请问一般搜索功能前后端数据是如何交互的呢?
有哪些思路?

怪我咯
怪我咯

走同样的路,发现不同的人生

reply all(3)
黄舟

Two implementation methods. If there are very few and stable entries, just get them as you do to reduce traffic and speed up search.
Usually, the search content is passed to the backend, and the backend returns the search results.

Ty80

Usually use HTTPRequest (NSURL, NSURLConnection, NSURLSession and other system built-in methods, or third-party libraries such as AFNetworking) to send a get or post request to the server, which contains the information you want to retrieve, such as product type, color, price etc., usually in JSON form. Then wait for the server to return the search results, usually in JSON format, and then display the content obtained from the server as the results.

大家讲道理

If it is an enumeration type data, the options are limited. Of course, it is all placed on the client and then retrieved.

If, as you said, it is a commodity and the quantity is large, then it can only be put on the server for retrieval. After the user enters the keywords and clicks search, the keywords are sent to the server, and the server returns a list of search results. After the client receives it, it is displayed.

There is another method that wastes a little more traffic, but the user experience will be better: while the user is typing, the keywords are sent to the server for retrieval, and the retrieval results are displayed. As the content entered by the user changes, the retrieval results change accordingly. You can check out the ReactiveCocoa framework, it has some examples:

let searchStrings = textField.rac_textSignal()
    .toSignalProducer()
    .map { text in text as! String }
    .throttle(0.5, onScheduler: QueueScheduler.mainQueueScheduler)
    
let searchResults = searchStrings
    .flatMap(.Latest) { (query: String) -> SignalProducer<(NSData, NSURLResponse), NSError> in
        let URLRequest = self.searchRequestWithEscapedQuery(query)

        return NSURLSession.sharedSession()
            .rac_dataWithRequest(URLRequest)
            .retry(2)
            .flatMapError { error in
                print("Network error occurred: \(error)")
                return SignalProducer.empty
            }
    }
    .map { (data, URLResponse) -> String in
        let string = String(data: data, encoding: NSUTF8StringEncoding)!
        return self.parseJSONResultsFromString(string)
    }
    .observeOn(UIScheduler())
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!