Most efficient way to store an often changed array of array of images in core data











up vote
0
down vote

favorite












So I have been doing research on how to store an array of images and I am currently using an extension method suggested in another stack overflow post.



extension Array where Element: UIImage {
// Given an array of UIImages return a Data representation of the array suitable for storing in core data as binary data that allows external storage
func coreDataRepresentation() -> ImageArrayRepresentation? {
let CDataArray = NSMutableArray()

for img in self {
guard let imageRepresentation = UIImagePNGRepresentation(img) else {
print("Unable to represent image as PNG")
return nil
}
let data : NSData = NSData(data: imageRepresentation)
CDataArray.add(data)
}

return NSKeyedArchiver.archivedData(withRootObject: CDataArray)
}
}

extension ImageArrayRepresentation {
// Given a Data representation of an array of UIImages return the array
func imageArray() -> ImageArray? {
if let mySavedData = NSKeyedUnarchiver.unarchiveObject(with: self) as? NSArray {
// TODO: Use regular map and return nil if something can't be turned into a UIImage
let imgArray = mySavedData.compactMap({
return UIImage(data: $0 as! Data)
})
return imgArray
}
else {
print("Unable to convert data to ImageArray")
return nil
}
}
}


So then when I save the array I simply use images.coreDataRepresentation() and store it in a binary data attribute of my entity.



My fear is that as the array may contain upwards of 900 images, and that constantly saving all of this data would result in delays. I was thinking that using a system similar to that of relational databases where I keep a separate core data entity to hold solely these photos with a relationship based on id, however, after doing more research I found that this is not advised with core data. Is there a better way to do this? Or is one of these methods superior?










share|improve this question






















  • How big are the images? Have you tested with 900 images to see how it performs?
    – Mike Taverne
    Nov 12 at 17:42










  • @MikeTaverne the images are taken directly from the camera or the photo library so depending on what version of iPhone the picture size may vary. It is also possible that there will be more than 900 images, although it will likely be uncommon.
    – joshLor
    Nov 12 at 17:53










  • @joshLor Wouldn’t it be better to store images inside Documents Directory and store a reference to them inside Core Data entity’s attribute
    – Shubham Bakshi
    Nov 12 at 18:17












  • @ShubhamBakshi What advantage does this give over just saving in core data
    – joshLor
    Nov 13 at 1:04










  • @joshLor Core Data is not generally used for storing multimedia content, as it is mainly for textual data and data that does not amount to too much in a single entry(like in a row) . On the other hand,Documents Directory,Tmp Directory are all there for multimedia content such as audio, video and images . I am not so sure about faster retrieval from Documents Directory as compared to Core Data in case of images but I think Documents Directory is faster. Plus it's a standard way of storing images. Plus you said you have 900 images, Documents Directory will be a safe bet
    – Shubham Bakshi
    Nov 13 at 4:56

















up vote
0
down vote

favorite












So I have been doing research on how to store an array of images and I am currently using an extension method suggested in another stack overflow post.



extension Array where Element: UIImage {
// Given an array of UIImages return a Data representation of the array suitable for storing in core data as binary data that allows external storage
func coreDataRepresentation() -> ImageArrayRepresentation? {
let CDataArray = NSMutableArray()

for img in self {
guard let imageRepresentation = UIImagePNGRepresentation(img) else {
print("Unable to represent image as PNG")
return nil
}
let data : NSData = NSData(data: imageRepresentation)
CDataArray.add(data)
}

return NSKeyedArchiver.archivedData(withRootObject: CDataArray)
}
}

extension ImageArrayRepresentation {
// Given a Data representation of an array of UIImages return the array
func imageArray() -> ImageArray? {
if let mySavedData = NSKeyedUnarchiver.unarchiveObject(with: self) as? NSArray {
// TODO: Use regular map and return nil if something can't be turned into a UIImage
let imgArray = mySavedData.compactMap({
return UIImage(data: $0 as! Data)
})
return imgArray
}
else {
print("Unable to convert data to ImageArray")
return nil
}
}
}


So then when I save the array I simply use images.coreDataRepresentation() and store it in a binary data attribute of my entity.



My fear is that as the array may contain upwards of 900 images, and that constantly saving all of this data would result in delays. I was thinking that using a system similar to that of relational databases where I keep a separate core data entity to hold solely these photos with a relationship based on id, however, after doing more research I found that this is not advised with core data. Is there a better way to do this? Or is one of these methods superior?










share|improve this question






















  • How big are the images? Have you tested with 900 images to see how it performs?
    – Mike Taverne
    Nov 12 at 17:42










  • @MikeTaverne the images are taken directly from the camera or the photo library so depending on what version of iPhone the picture size may vary. It is also possible that there will be more than 900 images, although it will likely be uncommon.
    – joshLor
    Nov 12 at 17:53










  • @joshLor Wouldn’t it be better to store images inside Documents Directory and store a reference to them inside Core Data entity’s attribute
    – Shubham Bakshi
    Nov 12 at 18:17












  • @ShubhamBakshi What advantage does this give over just saving in core data
    – joshLor
    Nov 13 at 1:04










  • @joshLor Core Data is not generally used for storing multimedia content, as it is mainly for textual data and data that does not amount to too much in a single entry(like in a row) . On the other hand,Documents Directory,Tmp Directory are all there for multimedia content such as audio, video and images . I am not so sure about faster retrieval from Documents Directory as compared to Core Data in case of images but I think Documents Directory is faster. Plus it's a standard way of storing images. Plus you said you have 900 images, Documents Directory will be a safe bet
    – Shubham Bakshi
    Nov 13 at 4:56















up vote
0
down vote

favorite









up vote
0
down vote

favorite











So I have been doing research on how to store an array of images and I am currently using an extension method suggested in another stack overflow post.



extension Array where Element: UIImage {
// Given an array of UIImages return a Data representation of the array suitable for storing in core data as binary data that allows external storage
func coreDataRepresentation() -> ImageArrayRepresentation? {
let CDataArray = NSMutableArray()

for img in self {
guard let imageRepresentation = UIImagePNGRepresentation(img) else {
print("Unable to represent image as PNG")
return nil
}
let data : NSData = NSData(data: imageRepresentation)
CDataArray.add(data)
}

return NSKeyedArchiver.archivedData(withRootObject: CDataArray)
}
}

extension ImageArrayRepresentation {
// Given a Data representation of an array of UIImages return the array
func imageArray() -> ImageArray? {
if let mySavedData = NSKeyedUnarchiver.unarchiveObject(with: self) as? NSArray {
// TODO: Use regular map and return nil if something can't be turned into a UIImage
let imgArray = mySavedData.compactMap({
return UIImage(data: $0 as! Data)
})
return imgArray
}
else {
print("Unable to convert data to ImageArray")
return nil
}
}
}


So then when I save the array I simply use images.coreDataRepresentation() and store it in a binary data attribute of my entity.



My fear is that as the array may contain upwards of 900 images, and that constantly saving all of this data would result in delays. I was thinking that using a system similar to that of relational databases where I keep a separate core data entity to hold solely these photos with a relationship based on id, however, after doing more research I found that this is not advised with core data. Is there a better way to do this? Or is one of these methods superior?










share|improve this question













So I have been doing research on how to store an array of images and I am currently using an extension method suggested in another stack overflow post.



extension Array where Element: UIImage {
// Given an array of UIImages return a Data representation of the array suitable for storing in core data as binary data that allows external storage
func coreDataRepresentation() -> ImageArrayRepresentation? {
let CDataArray = NSMutableArray()

for img in self {
guard let imageRepresentation = UIImagePNGRepresentation(img) else {
print("Unable to represent image as PNG")
return nil
}
let data : NSData = NSData(data: imageRepresentation)
CDataArray.add(data)
}

return NSKeyedArchiver.archivedData(withRootObject: CDataArray)
}
}

extension ImageArrayRepresentation {
// Given a Data representation of an array of UIImages return the array
func imageArray() -> ImageArray? {
if let mySavedData = NSKeyedUnarchiver.unarchiveObject(with: self) as? NSArray {
// TODO: Use regular map and return nil if something can't be turned into a UIImage
let imgArray = mySavedData.compactMap({
return UIImage(data: $0 as! Data)
})
return imgArray
}
else {
print("Unable to convert data to ImageArray")
return nil
}
}
}


So then when I save the array I simply use images.coreDataRepresentation() and store it in a binary data attribute of my entity.



My fear is that as the array may contain upwards of 900 images, and that constantly saving all of this data would result in delays. I was thinking that using a system similar to that of relational databases where I keep a separate core data entity to hold solely these photos with a relationship based on id, however, after doing more research I found that this is not advised with core data. Is there a better way to do this? Or is one of these methods superior?







swift core-data uiimage






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 12 at 17:31









joshLor

477316




477316












  • How big are the images? Have you tested with 900 images to see how it performs?
    – Mike Taverne
    Nov 12 at 17:42










  • @MikeTaverne the images are taken directly from the camera or the photo library so depending on what version of iPhone the picture size may vary. It is also possible that there will be more than 900 images, although it will likely be uncommon.
    – joshLor
    Nov 12 at 17:53










  • @joshLor Wouldn’t it be better to store images inside Documents Directory and store a reference to them inside Core Data entity’s attribute
    – Shubham Bakshi
    Nov 12 at 18:17












  • @ShubhamBakshi What advantage does this give over just saving in core data
    – joshLor
    Nov 13 at 1:04










  • @joshLor Core Data is not generally used for storing multimedia content, as it is mainly for textual data and data that does not amount to too much in a single entry(like in a row) . On the other hand,Documents Directory,Tmp Directory are all there for multimedia content such as audio, video and images . I am not so sure about faster retrieval from Documents Directory as compared to Core Data in case of images but I think Documents Directory is faster. Plus it's a standard way of storing images. Plus you said you have 900 images, Documents Directory will be a safe bet
    – Shubham Bakshi
    Nov 13 at 4:56




















  • How big are the images? Have you tested with 900 images to see how it performs?
    – Mike Taverne
    Nov 12 at 17:42










  • @MikeTaverne the images are taken directly from the camera or the photo library so depending on what version of iPhone the picture size may vary. It is also possible that there will be more than 900 images, although it will likely be uncommon.
    – joshLor
    Nov 12 at 17:53










  • @joshLor Wouldn’t it be better to store images inside Documents Directory and store a reference to them inside Core Data entity’s attribute
    – Shubham Bakshi
    Nov 12 at 18:17












  • @ShubhamBakshi What advantage does this give over just saving in core data
    – joshLor
    Nov 13 at 1:04










  • @joshLor Core Data is not generally used for storing multimedia content, as it is mainly for textual data and data that does not amount to too much in a single entry(like in a row) . On the other hand,Documents Directory,Tmp Directory are all there for multimedia content such as audio, video and images . I am not so sure about faster retrieval from Documents Directory as compared to Core Data in case of images but I think Documents Directory is faster. Plus it's a standard way of storing images. Plus you said you have 900 images, Documents Directory will be a safe bet
    – Shubham Bakshi
    Nov 13 at 4:56


















How big are the images? Have you tested with 900 images to see how it performs?
– Mike Taverne
Nov 12 at 17:42




How big are the images? Have you tested with 900 images to see how it performs?
– Mike Taverne
Nov 12 at 17:42












@MikeTaverne the images are taken directly from the camera or the photo library so depending on what version of iPhone the picture size may vary. It is also possible that there will be more than 900 images, although it will likely be uncommon.
– joshLor
Nov 12 at 17:53




@MikeTaverne the images are taken directly from the camera or the photo library so depending on what version of iPhone the picture size may vary. It is also possible that there will be more than 900 images, although it will likely be uncommon.
– joshLor
Nov 12 at 17:53












@joshLor Wouldn’t it be better to store images inside Documents Directory and store a reference to them inside Core Data entity’s attribute
– Shubham Bakshi
Nov 12 at 18:17






@joshLor Wouldn’t it be better to store images inside Documents Directory and store a reference to them inside Core Data entity’s attribute
– Shubham Bakshi
Nov 12 at 18:17














@ShubhamBakshi What advantage does this give over just saving in core data
– joshLor
Nov 13 at 1:04




@ShubhamBakshi What advantage does this give over just saving in core data
– joshLor
Nov 13 at 1:04












@joshLor Core Data is not generally used for storing multimedia content, as it is mainly for textual data and data that does not amount to too much in a single entry(like in a row) . On the other hand,Documents Directory,Tmp Directory are all there for multimedia content such as audio, video and images . I am not so sure about faster retrieval from Documents Directory as compared to Core Data in case of images but I think Documents Directory is faster. Plus it's a standard way of storing images. Plus you said you have 900 images, Documents Directory will be a safe bet
– Shubham Bakshi
Nov 13 at 4:56






@joshLor Core Data is not generally used for storing multimedia content, as it is mainly for textual data and data that does not amount to too much in a single entry(like in a row) . On the other hand,Documents Directory,Tmp Directory are all there for multimedia content such as audio, video and images . I am not so sure about faster retrieval from Documents Directory as compared to Core Data in case of images but I think Documents Directory is faster. Plus it's a standard way of storing images. Plus you said you have 900 images, Documents Directory will be a safe bet
– Shubham Bakshi
Nov 13 at 4:56



















active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














 

draft saved


draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53267267%2fmost-efficient-way-to-store-an-often-changed-array-of-array-of-images-in-core-da%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown






























active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes
















 

draft saved


draft discarded



















































 


draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53267267%2fmost-efficient-way-to-store-an-often-changed-array-of-array-of-images-in-core-da%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

How to send String Array data to Server using php in android

Title Spacing in Bjornstrup Chapter, Removing Chapter Number From Contents

Is anime1.com a legal site for watching anime?